Divide matrix by value in MATLAB

I wonder if there is a MATLAB solution for partitioning a matrix into submatrices, such as:

Matrix:

A = 16 2 3 5 11 10 9 7 6 4 14 15 5 1 3 

I would like to take lines starting at 5 on another, those starting at 16 on another, etc.

Is there a function for this or should I go with if / for the approach?

+4
source share
2 answers

Here is one solution that uses the SORTROWS , UNIQUE , ACCUMARRAY and MAT2CELL functions to create an array of cells with each cell storing a set of rows with the same value in the first column:

 >> sortedA = sortrows(A,1); %# Sort the rows by the first column >> [~,~,uniqueIndex] = unique(sortedA(:,1)); %# Find indices of unique values %# in the first column >> cellA = mat2cell(sortedA,... %# Break matrix up by rows accumarray(uniqueIndex(:),1),3); %# into a cell array >> cellA{:} %# Display the contents of the cells ans = 4 14 15 ans = 5 11 10 5 1 3 ans = 9 7 6 ans = 16 2 3 
+3
source

I think I found this =)

 for n=1:max(max(A)) M{n} = A(find(A(:,1)==n),:); end 

Now M{n} is the matrix of all rows starting with n . =)

+1
source

Source: https://habr.com/ru/post/1347804/


All Articles