Sum of an arrays elements

3 ビュー (過去 30 日間)
Rasmus
Rasmus 2014 年 2 月 23 日
コメント済み: Rasmus 2014 年 2 月 23 日
I have an assigntment which sounds like this:
square each element in C and determine the sum of all C's elements
How do i do this? I don't get the right result, that i am supposed to get.

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 23 日
編集済み: Mischa Kim 2014 年 2 月 23 日
I'd assume
C = [1 2 3];
res = sum(C.^2)
What is the input, what is the expected result?
  9 件のコメント
Mischa Kim
Mischa Kim 2014 年 2 月 23 日
A = [10 12 15; 7 9 13];
B = A';
C = ones(4,10);
C(2,5) = 8;
C(:,7) = zeros(4,1);
C(1:2,1:3) = A;
C(2:4,8:9) = B;
C(3,:) = [];
sum(sum(C.^2))
ans =
1391
Rasmus
Rasmus 2014 年 2 月 23 日
Hmm weird, that was pretty much what i was doing, but at least now i get the same result.
Thnak you :)

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

その他の回答 (1 件)

Sagar Damle
Sagar Damle 2014 年 2 月 23 日
Rasmus,entered matrix is wrong again! I get this fact from your description. Your matrix is - C =
10 12 15 1 1 1 0 1 1 1
7 9 13 1 8 1 0 10 7 1
1 1 1 1 1 1 0 15 13 1
Any way,I think this is what you want :
A = C.^2; Ans = sum(A(:));
For understanding purpose,see effect of A(:).
Note : For 2-dimentional matrix A, sum(sum(A)) is same as sum(A(:)).
Read help about 'sum()' in MATLAB help.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by