How to sum up row values in a matrix?

Dear All
I have matrix A
A = [ 1 2 3 5;
3 4 5 4;
];
I want to add row values like that using a loop ( without manual input)
A(1,1) + A(1,2) = B1
A(1,3) + A(1,4) = B2
A(2,1) + A(2,2) = B3
A(2,3) + A(2,4) = B4
B= [ B1 B2;
B3 B4
];
How can I do that any tips
Many Thanks in advance

6 Comments

Looks like you've done it (almost) except that you need to flip your B's to the other side:
B1 = A(1,1) + A(1,2);
B2 = A(1,3) + A(1,4);
B3 = A(2,1) + A(2,2);
B4 = A(2,3) + A(2,4);
B= [ B1 B2;
B3 B4
];
No loop needed (for such a small array). Why do you want to use a loop?
Can you tell how to use a loop? Sorry for the late comment
m = randi(4, 3, 5); % Sample data in a 3 by 5 matrix
[rows, columns] = size(m) % Get dimensions of the matrix.
% Preallocate space for the sums of the rows.
rowSums = zeros(rows, 1);
for row = 1 : rows
% Get the sum for this row across all columns in this row.
for col = 1 : columns
rowSums(row) = rowSums(row) + m(row, col);
end
% Print the sums to the command window
fprintf('For row #%d the sum over the columns = %f.\n', row, rowSums(row));
end
Yuli Hartini
Yuli Hartini on 2 Jan 2017
Edited: Yuli Hartini on 2 Jan 2017
What if I have matrix M, M = [1 2 0.2; 2 3 0.1; 3 4 0.4] And I want values like this.. Values= [0.2; (0.2+01); (0.2+0.1+0.4)]
Help me please
I'm not sure of your rule, but it looks like you might be doing
Values = cumsum(M(:, end))

Sign in to comment.

 Accepted Answer

I have a matrix like [1 2 3 4] I want an output [1 3 6 10]

More Answers (4)

A = [ 1 2 3 5;3 4 5 4]
res=reshape(sum(reshape(A',1,2,[])),2,2)'
%or
res=A(:,[1 3])+A(:,[2 4])
%or
n=size(A,2)/2
res=[sum(A(1,1:n)) sum(A(1,n+1:end)); sum(A(2,1:n)) sum(A(2,n+1:end))]
Image Analyst
Image Analyst on 14 Sep 2012
Edited: Image Analyst on 14 Sep 2012
Here's one way:
A = [ 1 2 3 5;
3 4 5 4]
% Get the sliding sum.
a2 = conv2(A, [1 1], 'valid');
% Extract just the first and last column.
output = [a2(:,1) a2(:,3)]
Hi Image analyst
I have bigger matrix.
A=
0.0018 0.0008 0.0000 0.0000 0.2304 0.7345 0.0159 0.0166
0.0024 0.0016 0.0001 0.0000 0.2161 0.7441 0.0165 0.0192
0.0029 0.0027 0.0002 0.0000 0.2084 0.7475 0.0169 0.0214
0.0034 0.0040 0.0003 0.0000 0.2041 0.7479 0.0172 0.0230
0.0038 0.0055 0.0005 0.0001 0.2016 0.7468 0.0175 0.0243
0.0041 0.0072 0.0007 0.0001 0.1999 0.7450 0.0177 0.0253
0.0044 0.0090 0.0009 0.0001 0.1988 0.7429 0.0178 0.0261
I want to do the operation like your code
% Get the sliding sum
a2 = conv2(A, [1 1], 'valid');
how can I do that
Here I want have to add
A(1,1) + A(1,2) + A(1,3)+ A(1,4) = B1
A(1,5) + A(1,6) + A(1,7)+ A(1,8) = B2
A(2,1) + A(2,2) + A(2,3)+ A(2,4) = B3
A(2,5) + A(2,6) + A(2,7)+ A(2,8) = B4
B = [ B1 B2
B3 B4]
Thanks

4 Comments

B=A(1:2,:)
n=size(B,2)/2
res=reshape(sum(reshape(B',1,n,[])),2,2)'
See my other answer. I was wondering - but usually when it needs to be general for some variable number of rows or columns, people will say that in advance so they get the general answer the first time.
or simpler
n=size(A,2)/2
res=[sum(A(1,1:n)) sum(A(1,n+1:end)); sum(A(2,1:n)) sum(A(2,n+1:end))]
Yeah, that's probably better - more direct - as long as he has a 2 row array. In his example here (which he incorrectly posted as an answer), he has a 7 row by 8 column array. See my build on your solution for when it has any number of rows.

Sign in to comment.

Image Analyst
Image Analyst on 14 Sep 2012
Edited: Image Analyst on 14 Sep 2012
A=[...
0.0018 0.0008 0.0000 0.0000 0.2304 0.7345 0.0159 0.0166
0.0024 0.0016 0.0001 0.0000 0.2161 0.7441 0.0165 0.0192
0.0029 0.0027 0.0002 0.0000 0.2084 0.7475 0.0169 0.0214
0.0034 0.0040 0.0003 0.0000 0.2041 0.7479 0.0172 0.0230
0.0038 0.0055 0.0005 0.0001 0.2016 0.7468 0.0175 0.0243
0.0041 0.0072 0.0007 0.0001 0.1999 0.7450 0.0177 0.0253
0.0044 0.0090 0.0009 0.0001 0.1988 0.7429 0.0178 0.0261]
[rows columns] = size(A)
% Get the sliding sum
a2 = conv2(A, ones(1, columns/2), 'valid')
% Extract just the first and last column.
B = [a2(:,1) a2(:,end)]
Or, building off Azzi's solution and making it work for a 2D array of any number of rows:
B = [sum(A(:,1:columns/2), 2) sum(A(:,(columns/2)+1:end), 2)]
This is probably the most direct way. And it's only 1 line of code instead of 2.

Categories

Find more on Loops and Conditional Statements 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!