MATLAB: Partitioning a matrix based on another

MATLABpartition

I have two matrices,say, X=[1 2 2; 2 3 3; 3 5 5 ;6 1 2; 3 4 3] and ng=[2;3]. I want to partition X based on ng, i.e.
x1=[1 2 2; 2 3 3], i.e. taking the ng(1) number of rows x2=[3 5 5 ;6 1 2; 3 4 3] , ng(2) number of rows where
X=[x1;x2].
I can do it using a for loop, but is there any efficient way to do this? This is just an example, there can be a larger ng, and also x

Best Answer

k = mat2cell(X,ng,size(X,2))
[x1,x2] = k{:}
Related Question