MATLAB: How to group elements of matrix

matrixsubset

I have a row matrix
A=
0.8417 0.0552 0.1037 0.3880 0.3856 0.9601 0.8082 0.5269 0.4801 0.2059 0.9894 0.8849 0.7174 0.3461 0.8947 0.1219 0.0567 0.7508 0.5415 0.0916 0.0778 0.6777 0.5193 0.5166 0.8762 0.6615 0.1384 0.0826 0.4404 0.5334 (row matrix A=1*30)
Now I want to make subsets of A containing 3 elements each for example
A1= 0.8417 0.0552 0.1037 (row matrix A1=1*3)
A2=0.3880 0.3856 0.9601 and so on..
Which command should I use?
Thanks for the help.

Best Answer

The reshape function is especially useful, since you don't have to determine the size of the other dimension yourself:
A=rand(1,30);
groupsize=3;
B=reshape(A,[],groupsize);
Alternatively, you can also use a cell array:
A=rand(1,30);
groupsize=3;
C=mat2cell(A,1,groupsize*ones(numel(A)/groupsize,1));