How to sum elements of matrix in certain condition?

2 ビュー (過去 30 日間)
Jenjen Ahmad Zaeni
Jenjen Ahmad Zaeni 2022 年 1 月 16 日
コメント済み: Jenjen Ahmad Zaeni 2022 年 1 月 16 日
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
Cris LaPierre 2022 年 1 月 16 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by