# How to sum elements of matrix in certain condition?

4 views (last 30 days)
Jenjen Ahmad Zaeni on 16 Jan 2022
Hello everyone. I'm sorry if i didn't explain it clearly. I have a matrix like
A =
1.0999 -1.0999 -2.0590 0 -1.0999 0 0
0 0 0.1168 -0.1168 0 -1.0999 0
-2.4128 0 0 -5.2622 0 0 2.4128
I want a new matrix with the same size. The element of new matrix is a sum of other elements in the same column. For example, A(1,1) = 1.0999. The new matrix B(1,1) is a sum of 0 and -2.4128, so B(1,1)=-2.4128. Based on that condition, the new desired matrix is
B =
-2.4128 0 0.1168 -5.3790 0 -1.0999 2.4128
-1.3129 -1.0999 -2.0590 -5.2622 -1.0999 0 2.4128
1.0999 -1.0999 -1.9422 -0.1168 -1.0999 -1.0999 0
All answer will be appreciated. Thank you very much.

Cris LaPierre on 16 Jan 2022
A = [1.0999 -1.0999 -2.0590 0 -1.0999 0 0;
0 0 0.1168 -0.1168 0 -1.0999 0;
-2.4128 0 0 -5.2622 0 0 2.4128];
B = [sum(A([2 3],:)); sum(A([1 3],:)); sum(A([1 2],:))]
B = 3×7
-2.4128 0 0.1168 -5.3790 0 -1.0999 2.4128 -1.3129 -1.0999 -2.0590 -5.2622 -1.0999 0 2.4128 1.0999 -1.0999 -1.9422 -0.1168 -1.0999 -1.0999 0
Jenjen Ahmad Zaeni on 16 Jan 2022
Thank you very much!

R2016a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by