howto organize an array into groups of n-Elements

Let's say I have a Vector of length 100
x = 1:100
and want to reshape that Vector into a 5x20 Matrix. 20 Columns with 5 Elements per Column.
a = reshape (x,5,20)
There are, then, 4 Variations of this. Starting at the 1st, 2nd, 3rd, or 4th Element.
I can group Elements 1-5 into the 1st Column, Elements 6-10 into the 2nd Column, etc.
circshift (x, -1)
b = reshape (x,5,20)
But I can also group Elements 2-6 into the 1st, 7-11 into the next, etc. The last column containing the 4 last Elements of the Vector, plus the first.
circshift (x, -2)
c = reshape (x,5,20)
I can also group Elements 3-7, 8-12, last column containing the 3 last Elements and the first 2.
circshift (x, -3)
d = reshape (x,5,20)
Then lastly 4-8, 9-13 etc.
circshift (x, -4)
e = reshape (x,5,20)
As the Order of the Columns (or Order of the Elements in the Columns) doesn't matter, circshift (x, +-5) has the same information than the original Vector x.
Is there a more Elegant Solution than circshift-ing the Vector (x) 4 times separately, and reshaping it into a matrix 4 times separately? Some function which produces from this Vector x the 4 Matrices a, b, c & d ?
I can't think of a proper title for this question, which really bugs me. Feel free to offer a suggestion.

 Accepted Answer

Another way:
x = 1:100;
a = reshape (x,5,20);
aa = [a;a];
v = cell(1,5);
for k=1:5
v{k} = aa(k:k+4,:);
end
Then the v{k} are your variables.

3 Comments

Not quite, this cycles through the order of elements in each column individually.
so where v{1} will return the first as Column 1 2 3 4 5, (and the 2nd as 6 7 8 9 10),
v{2} returns it as 2 3 4 5 1 (and the 2nd Column as 7 8 9 10 6),
v {3} returns it as 3 4 5 1 2, (and the 2nd Column as 8 9 10 6 7). etc.
So the Top Element in each Column always jumps to the Bottom Position.
I want to cycle the Elements through the whole Matrix.
so where v{1} will also return 1 2 3 4 5 in the 1st Column,
v{2} returns 99 1 2 3 4 (and the 2nd Column 5 6 7 8 9)
v{3} returns 98 99 1 2 3 (and the 2nd Column 4 5 6 7 8).
The Bottom Element in each Column, so to speak, always jumps to the top of the adjacent Column. The Bottom Element in the Last Column jumps to Top Position in the First Column. Until, when cycling through this 6 Times, you have a Matrix with the initially last Column as the first.
The other way around works, too, with the first Element in each column Jumping to the Bottom of the Previous Column. Cycling through this 6 Times yields a Matrix which 'starts' with the initial 2nd Column, and the initial 1st Column in last Place.
How about with this slight edit?
x = 1:100;
a = reshape (x,5,20);
aa = [a;circshift(a,-1,2)];
v = cell(1,5);
for k=1:5
v{k} = aa(k:k+4,:);
end
That did the trick, thank you both most kindly. Love this Community <3

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!