split matrix in diffrent sizes matrixes according to indicies vector
Show older comments
I have a matrix A (7x2) 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.
Accepted Answer
More Answers (1)
the cyclist
on 25 Sep 2019
Stephen's solution is better, but I thought I would post this for loop version, which might be a bit clearer to you on what is happening:
A = [2 6;
4 3;
3 3;
2 8;
9 1;
3 4;
7 5];
B=[2; 3; 6];
C = [0; B; size(A,1)];
for nj = 1:numel(C)-1
An{nj} = A(C(nj)+1:C(nj+1),:);
end
I would also advocate using cell arrays rather than dynamically named variables.
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!