MATLAB: Splitting 1 column array

columnsplit

Hi all,
I have various column matrices with varying number of elements from 38-202. I want to split each matrix into 5 small matrices each containing the same number of elements. The distribution of numbers into each can be random. If the original matrix has more elements, some can be ignored as well. Like for example if the original matrix has 204 elements and I want to split it into 5 matrices. So only 200 elements can be shuffled and 4 need to be ignored.
Is there any function in MATLAB I can use?
Nancy

Best Answer

num_subsets = 5;
matlen = length(YourVector);
num_to_use = matlen - mod( matlen, num_subsets );
idx = randperm(matlen);
shuffled = YourVector(idx(1:num_to_use));
subsets = reshape(shuffled, [], num_subsets);
The subsets would then be the columns.