Hello everyone, I want to split the matrix in such a way that when elements of 1st column are equal, the correspnding elements can be splitted into another matrix. For example, i have the following matrix.
A = [ 1 2; 1 3; 1 4; 2 1; 2 4].
i want to get the following 2 matrices as the resulting matrices.
a1 = [2;3;4] and a2= [1;4]
i have just started learning the looping in MATLAB, your coperation will be highly appreciated.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 22일
편집: Azzi Abdelmalek 2016년 8월 22일

0 개 추천

A = [ 1 2; 1 3; 1 4; 2 1; 2 4]
[ii,jj,kk]=unique(A(:,1),'stable')
out=accumarray(kk,A(:,2),[],@(x){x})
celldisp(out)

댓글 수: 4

pradeep kumar
pradeep kumar 2016년 8월 22일
Thanks Azzi. That was a great help indeed. but , please note that in my actual problem A is a huge matrix of order (100 X 2). Is there anyway to use the accumarray inside a loop ? kindly help me.
Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 22일
편집: Azzi Abdelmalek 2016년 8월 22일
Relatively, 100x2 is not Huge. And how do you want to use the loop?
pradeep kumar
pradeep kumar 2016년 8월 22일
편집: pradeep kumar 2016년 8월 22일
I have no idea . So i am asking, is there any way ?
Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 22일
I mean your question is not clear, why do you need a for loop, when accumarray can do the job?

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

추가 답변 (1개)

Stephen23
Stephen23 2016년 8월 22일

1 개 추천

Use accumarray:
out = accumarray(A(:,1),A(:,2),[],@(v){v})

댓글 수: 1

pradeep kumar
pradeep kumar 2016년 8월 22일
Thanks Stephen. That was a great help indeed. but , please note that in my actual problem A is a huge matrix of order (100 X 2). Is there anyway to use the accumarray inside a loop ? kindly help me.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2016년 8월 22일

댓글:

2016년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by