Sorting and collecting data from arrays

I have two columns which contain data and I am struggling to figure out how you could design code to run through the matrix and select the groups of numbers with their corrsposning 2nd column number.
For example a matrix has to 2 columns and 10 rows and each number out of the first coulmn has its corrosponding number out of the second column.
What I want to do is to take all the 1's and their corrspronding value in the 2nd column and put them into a seperate matrix, and then take all the 2's with there corrsponding values and put them into a matrix and so...
A(:,1)=[1; 1; 1; 1; 2; 2; 2; 2; 3; 3]
A(:,2)=[9; 4; 6; 3; 6; 7; 6; 4; 9; 4]
Any help would be great cheers!

 採用された回答

Iain
Iain 2013 年 6 月 7 日

0 投票

range = unique(A(:,1));
for i = 1:numel(range)
collection{i} = A(range(i) == A(:,1),2);
end

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 7 日
編集済み: Azzi Abdelmalek 2013 年 6 月 7 日

0 投票

out=arrayfun(@(x) A(find(A(:,1)==x),:),unique(A(:,1)),'un',0)
% the result
out{1}
out{2}
out{3}

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2013 年 6 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by