addition of elements in a matrix
2 views (last 30 days)
Show older comments
Hi I have a matrix, e.g. "A" whose dimension is 3*20. I need to obtain a 3*4 matrix ("B") out of it, wherein every fifth element in a row is added together. That means the new matrix B11= A11+A15+A110+A120 and so on. Since the actual dimension of A is really huge, I need to do this in a loop. Any help will be appreciated.
Thanks, Subrat
0 Comments
Accepted Answer
Andrei Bobrov
on 16 Aug 2012
Edited: Andrei Bobrov
on 16 Aug 2012
Can so?
A = randi(30,3,20);
B = sum(reshape(A,size(A,1),4,[]),3);
OR
B2 = squeeze(sum(reshape(A,size(A,1),5,[]),2));
1 Comment
More Answers (1)
Wayne King
on 16 Aug 2012
Does this work for you?
A = randn(3,20);
B = zeros(3,4);
first = 1:5:size(A,2);
last = 5:5:size(A,2);
kk = 1;
for nn = 1:length(first)
tmp = A(:,first(nn):last(nn));
B(:,kk) = sum(tmp,2);
kk = kk+1;
end
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices 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!