フィルターのクリア

problems with matrix splitting

1 回表示 (過去 30 日間)
pradeep kumar
pradeep kumar 2016 年 8 月 22 日
コメント済み: Azzi Abdelmalek 2016 年 8 月 22 日
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 日
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 日
編集済み: 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 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by