MATLAB: Split matrix in diffrent sizes matrixes according to indicies vector

according incicies vectorsplit matrix

I have a matrix A (7×2) and a vector B. B represents the row indicies, where i want the matrix A to be sprlit.
e.g.
A = [2 6; 4 3; 3 3; 2 8; 9 1; 3 4; 7 5] B=[2; 3; 6]
output should be
A1 = [2 6; 4 3]
A2 = [3 3]
A3 = [2 8; 9 1; 3 4]
A4 = [7 5]
Do you have an idea how it could be done?
Thank you for help.

Best Answer

Do NOT create numbered variables. Indexing is simpler and much more efficient.
You can use mat2cell to split the matrix up into a cell array:
>> R = diff([0;B;size(A,1)]);
>> C = mat2cell(A,R,2);
>> C{:}
ans =
2 6
4 3
ans =
3 3
ans =
2 8
9 1
3 4
ans =
7 5
and then you can trivially access the contents of that cell array using indexing:
E.g.:
>> C{2}
ans =
3 3
>> C{3}
ans =
2 8
9 1
3 4