Sum and Difference across row and column

Let say there is square matrix of order n-by-n, and n is odd integer
A = rand(3)
A =
0.4218 0.9595 0.8491
0.9157 0.6557 0.9340
0.7922 0.0357 0.6787
I want to add the entries of second column and take difference of entries in second row to the middle number, i.e.
0.6557+0.9595+0.0357-0.9157-0.9340 = -0.1988
How can I do?

 採用された回答

Stephen23
Stephen23 2020 年 5 月 8 日

0 投票

>> sum(A(:,2))-A(2,1)-A(2,3)
ans = -0.19880

3 件のコメント

Muhammad Usman
Muhammad Usman 2020 年 5 月 8 日
I need a general form of this formula, like for a matrix of 11-by-11?
Stephen23
Stephen23 2020 年 5 月 8 日
編集済み: Stephen23 2020 年 5 月 8 日
For any square matrix where N is odd:
>> N = 3; % e.g. size(A,1)
>> X = 1+(N-1)/2; % the middle
>> sum(A(:,X))-sum(A(X,[1:X-1,X+1:N]))
ans = -0.19880
Muhammad Usman
Muhammad Usman 2020 年 5 月 8 日
Thanks

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 5 月 8 日

0 投票

You can do this with two simple sum calls and two basic arithmetic operations if you use the inclusion-exclusion principle. Since this sounds like it may be a homework assignment I'm not going to give the complete answer.

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2020 年 5 月 8 日

回答済み:

2020 年 5 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by