So I let say there a matrix like this
b = [1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9]
I want to change b into a matrix like this
b = [6 15 24; 6 15 24; 6 15 24]
So I'm add summing up the columns, but only 3(e) at a time, leaving me with a matrix that used to be nxm but is now nx(m/e).
Currently, I'm doing this with a for loop where I just take every 3 columns together to make a new column and update the field of a preallocated matrix of size nx(m/3). However, I realize that I need to make it work for any e(not just 3) and because of this, and the large datasets I will be dealing with(both n and m are large), I need to be able to do this without a for loop.
Is there some built in function that will help me? I've tried going through the documentation but I can't seem to find anything that will work.

 Réponse acceptée

Star Strider
Star Strider le 3 Nov 2017
I’m not certain how well this will generalise, but it works with your current problem:
b = [1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9];
e = 3;
br = reshape(b, size(b,1), e, []);
Result = squeeze(sum(br,2))
Result =
6 15 24
6 15 24
6 15 24

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by