0
votes

I have a dataset/Matrix A, with the following features:

A = [ 
      2 3 6 2 2;
      5 3 5 6 2;
      4 5 6 5 2;
      6 4 3 2 0;
      2 3 6 2 0;
      5 3 5 6 0;
      4 5 6 5 2;
      6 4 3 2 2;
      2 3 6 2 2;
      5 3 5 6 2
];

In the last column, I have entries with 2 and 0. I want to split the matrix A into 3 different junks depending on the last column entries 2, 0 and again 2.

Could you please suggest any efficient way to do that??

I would be highly obliged for your earliest response.

1
What's the desired result? Cell array of submatrices, right?Luis Mendo
Hi! It's very good you have posted input example, but if you want to be helped you also need to post desired output. This will allow to understand what to you need much more faster without any additional questionsKonstantin Vustin
I apologize for the inconvenience. The desired output would be submatrix_a1 = 2 3 6 2 2 5 3 5 6 2 4 5 6 5 2Sushil Surwase
The desired output would be submatrix_a1 = [2 3 6 2 2; 5 3 5 6 2; 4 5 6 5 2] submatrix_a2 = [ 6 4 3 2 0; 2 3 6 2 0; 5 3 5 6 0 ]. submatrix_a3 = [ 4 5 6 5 2; 6 4 3 2 2; 2 3 6 2 2; 5 3 5 6 2].Sushil Surwase

1 Answers

3
votes

I assume the desired result is a cell array of (sub)matrices:

B = mat2cell(A, diff([0; find([diff(A(:,end)); 1])]), size(A,2));

For your example A, this gives

>> celldisp(B)
B{1} =
     2     3     6     2     2
     5     3     5     6     2
     4     5     6     5     2
B{2} =
     6     4     3     2     0
     2     3     6     2     0
     5     3     5     6     0
B{3} =
     4     5     6     5     2
     6     4     3     2     2
     2     3     6     2     2
     5     3     5     6     2