Sum of columns in a mxn Matrix on matlab

6 ビュー (過去 30 日間)
test test
test test 2022 年 10 月 24 日
コメント済み: dpb 2022 年 10 月 25 日
function sum =column_sum(A,cl)
sum=[];
[m,n]=size(A);
if cl==n
sum=A(cl,:)';
else
s=column_sum(A,cl+1);
for i=1:n
sum=[sum;s+A(cl,i);];
end
end
end
This is my code. cl stands for current line. But im getting the error:
error: 'A' undefined near line 3, column 12
error: called from
column_sum at line 3 column 6
>>
  2 件のコメント
Matt J
Matt J 2022 年 10 月 24 日
I'm getting no such error.
A=rand(4);
column_sum(A,3)
ans = 16×1
0.3737 0.2950 0.9750 0.8340 0.4587 0.3800 1.0600 0.9190 1.1090 1.0303
function sum =column_sum(A,cl)
sum=[];
[m,n]=size(A);
if cl==n
sum=A(cl,:)';
else
s=column_sum(A,cl+1);
for i=1:n
sum=[sum;s+A(cl,i);];
end
end
end
test test
test test 2022 年 10 月 24 日
Maybe the error has something to do with file paths then. Does it actually do what i want? Sum of the columns of the mxn matrix?

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

採用された回答

Image Analyst
Image Analyst 2022 年 10 月 24 日
Why have a function to sum a column? Why not simply do
columnSum = sum(A(:, cl), 1)
  8 件のコメント
Image Analyst
Image Analyst 2022 年 10 月 25 日
編集済み: Image Analyst 2022 年 10 月 25 日
To sum an entire matrix you can use
S = sum(A(:))
A Mathworker told me there is no slowness introduced by using (:).
If you really want the for loop way, you can do it with a single for loop
S = 0
for k = 1 : numel(A)
S = S + A(k);
end
Or you can use the 'all' option of sum
S = sum(A, 'all');
dpb
dpb 2022 年 10 月 25 日
A Mathworker told me there is no slowness introduced by using (:)."
Agreed, the use of the (:) is not terrible choice as compared to using the 'all' keyword; I could have made that proscription a little less hard. The nested sum(sum()) is an ugly hack still, however, seen far too often even in new code the idiom undoubtedly having been picked up by seeing it in older code or from the self-taught instructor who learned it the same way and never improved their own coding style and continues to pass on the same bad habits (as well as the penchant for "clear all" everywhere, even inside functions).
Sorry about the typo Indeed...I swear the Answers interface doesn't always catch keystrokes when typing...I discover a tremendous number of typos that I would swear I didn't make... :)

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

その他の回答 (1 件)

dpb
dpb 2022 年 10 月 24 日
s=column_sum(A,cl+1);
Will crash if pass cl > size(A,1) as there's no error checking for cl being out of range.
As for the Q? "Does it actually do what i want? Sum of the columns of the mxn matrix?" that depends on the actual definition intended. If it is to return simply the sum of a given row in the array A, then all that is needed is
function ans=column_sum(A,cl) % don't overload the builtin sum method
m=size(A,1); % number rows in A
assert(m>=cl,'Error: passed row greater than array size')
ans=sum(A(cl,:));
end
It's not clear at all beyond that what your expectations are -- by the recursive call you have, as @Matt J's example shows, you're returning a vector the size of the input array.
To return the sum of all rows in the array, just use the optional dim argument in sum as
col_sum=sum(A,2); % all rows summed by column dimension
If the idea is to only return those from the specified row to the end and not the full array size, then
col_sum=sum(A(cl:end,:),2); % all rows summed by column dimension from row cl:end
or simply
col_sum=sum(A,2); % all rows summed by column dimension
col_sum=col_sum(cl:end); % return only from row cl:end
You'll have to 'splain just what it is that you're really after explicitly; it's ambiguous as given.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by