Sum of columns in a mxn Matrix on matlab
23 views (last 30 days)
Show older comments
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 Comments
Matt J
on 24 Oct 2022
I'm getting no such error.
A=rand(4);
column_sum(A,3)
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
Accepted Answer
Image Analyst
on 24 Oct 2022
Why have a function to sum a column? Why not simply do
columnSum = sum(A(:, cl), 1)
8 Comments
Image Analyst
on 25 Oct 2022
Edited: Image Analyst
on 25 Oct 2022
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
on 25 Oct 2022
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... :)
More Answers (1)
dpb
on 24 Oct 2022
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.
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.
0 Comments
See Also
Categories
Find more on Holidays / Seasons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!