Hi, I have a question regarding sorting arrary and store them into subset. How can I sort an array according to the second column value and store them into subarrays. and get the value of second column. So for an arrary like the following, I would know there are 3 groups(1,2,3) according column 2 and I would get 3 subsets. Thanks a lot.
if true
1 1 9999.69307 3.374E-28
1 1 9999.72441 4.662E-27
1 2 9999.73112 2.083E-29
1 2 9999.93918 4.851E-29
1 1 9999.95707 1.353E-26
1 3 9999.69307 3.374E-28
1 1 9999.72441 4.662E-27
1 2 9999.74112 2.083E-29
1 3 9999.98918 4.851E-29
1 1 9999.99707 1.353E-26 end

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 31 日
編集済み: Azzi Abdelmalek 2014 年 4 月 1 日

0 投票

A = [ 1 1 9999.69307 3.374E-28
1 1 9999.72441 4.662E-27
1 9 9999.73112 2.083E-29
1 9 9999.93918 4.851E-29
1 1 9999.95707 1.353E-26
1 3 9999.69307 3.374E-28
1 5 9999.72441 4.662E-27
1 2 9999.74112 2.083E-29
1 3 9999.98918 4.851E-29
1 3 9999.99707 1.353E-26];
B=sortrows(A,2); % Sort A according to second column;
out=accumarray(A(:,2),(1:size(A,1))',[],@(x){A(x,:)})
out(cellfun(@isempty,out))=[]

1 件のコメント

Chong Tao
Chong Tao 2014 年 3 月 31 日
Thank you Azzi. This works perfect.

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

その他の回答 (2 件)

Chong Tao
Chong Tao 2014 年 3 月 31 日
編集済み: Chong Tao 2014 年 3 月 31 日

0 投票

Thank you very much Azzi and Per isakson! I need to clarify my question. I'm trying to load a pretty big array from file(3000X10 array) and sort the array. the second column may not be consective numbers as in previous sample. It can be any number from 1 to 11 and can't be predicted unless the data was loaded. say for example,
if true
A = [ 1 1 9999.69307 3.374E-28
1 1 9999.72441 4.662E-27
1 9 9999.73112 2.083E-29
1 9 9999.93918 4.851E-29
1 1 9999.95707 1.353E-26
1 3 9999.69307 3.374E-28
1 5 9999.72441 4.662E-27
1 2 9999.74112 2.083E-29
1 3 9999.98918 4.851E-29
1 3 9999.99707 1.353E-26];
end

1 件のコメント

Chong Tao
Chong Tao 2014 年 3 月 31 日
編集済み: Chong Tao 2014 年 3 月 31 日
all these are double precision numbers. is that what you ask?

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

Jos (10584)
Jos (10584) 2014 年 4 月 1 日

0 投票

Shorter, with less overhead and more flexible:
[~,~,j] = unique(A(:,2))
C = accumarray(j,1:numel(j),[max(j) 1],@(k) {A(k,:)})

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by