MATLAB: How to change array format – change row number and column number, but have all the data as before

arrays

Hi everyone,
I am interested in following if I put as an example;
I have an array A;
1 1 1 1
1 1 1 1
1 1 1 1
2 2 2 2
2 2 2 2
2 2 2 2
3 3 3 3
3 3 3 3
3 3 3 3
Now I would like to change this to array B which would look like;
1 1 1 1 2 2 2 2 3 3 3 3
1 1 1 1 2 2 2 2 3 3 3 3
1 1 1 1 2 2 2 2 3 3 3 3
How can I do this type of thing on matlab? The array A can have much more sets of data, and the code must go through each row of A until there is no more data.
Thanks a lot in advance! Sara

Best Answer

Depending on the required element order, (along 2nd dim first):
B = reshape(A.',3,[])
or (along 1st dim first):
B = reshape(permute(reshape(A,3,[],4),[1,3,2]),3,[])
or (along 1st dim first):
cell2mat(mat2cell(A,[3,3,3],4).')