Create n matrix from one matrix according to elements of a column?

조회 수: 2 (최근 30일)
I have a sorted matrix (according to the element of the third column)
A = [ 4 2 0 ;3 7 0 ; 8 9 3 ; 7 5 3; 4 4 3]
I want to get n matrix (in this case 2) from A, according to the value of the element of the third column, to get
M1 = [ 4 2 0 ; 3 7 0]
M2 = [8 9 3 ; 7 5 3; 4 4 3].
I know how many matrix I will have and the different elements to study using the function 'unique' that provide me a matrix (for this case) B=[0;3].
I tried with the functions 'genvarname' and 'eval' but I didn't find a good solution.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 4월 15일
편집: Andrei Bobrov 2016년 4월 15일
[~,~,c] = unique(A(:,end));
M = accumarray(c,(1:size(A,1))',[],@(x){A(x,:)});
  댓글 수: 2
Sleh Eddine Brika
Sleh Eddine Brika 2016년 4월 15일
It's so wonderful thank you !A really interesting command, but I really need to get separated Matrix and do you know how to dynamically generate matrix name
Andrei Bobrov
Andrei Bobrov 2016년 4월 15일
I am corrected. Thank you Azzi

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 15일
A = [ 4 2 0 ;3 7 0 ; 8 9 3 ; 7 5 3; 4 4 3]
[ii,jj,kk]=unique(A(:,3))
out=accumarray(kk,1,[],@(x) {A(x,:)})
% the first matrix and the second are
out{1}
out{2}
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 15일
@Sleh Eddine, it's easier to call your matrices with one variable and indices: out{1}, out{2},... then using multiple variables!
Sleh Eddine Brika
Sleh Eddine Brika 2016년 4월 16일
Ok, I will do it like you said ,thank you very much for your help Mr Azzi

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by