having problem to solve somthing in matlab
Show older comments
i have this question it says that i have a matrix A [m,n]
and i need to get the amount of the first row and the last row for example if i have a matrix like this :
7 5 3
9 1 8
10 1 3
i wanna get the first row ==> 7+5+3 =15
and the last row ==>10+1+3=14
and after that i need to do 15+14=29 ( as first row + the last row)
so the problem i have that i cant use sum function but i can use for
can anyone help me with this ?
Accepted Answer
More Answers (1)
S = sum(x)
is equivalent to
S = 0;
for k = 1:numel(x)
S = S + x(k);
end
If you want to, you can call this as a subfunction. Or you can collect the 2 sums in one loop:
Sfirst = 0;
Slast = 0;
[s1, s2] = size(matrix);
Then run a loop over the columns of the matrix and accumulate the 2 sums.
1 Comment
Jon
on 2 Jul 2019
I was thinking the OP was specifically looking for a solution that did not utilize either the sum function or looping, which is why I suggested the matrix multiply approach.
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!