How can I sort an array of repeating elements

조회 수: 1 (최근 30일)
mohan
mohan 2013년 4월 1일
Hi
I have the following array (20 by 2). The 1st column is node number and 2nd is temperature. The temperature is in descending order. There are 6 different values of temperatures. How can I sort this array such that it outputs;
set1 temp=670K nodes=10,14,15
set2 temp=668K nodes=385,431,432,440,441,442
set3 temp=655K nodes=649,650,959,960,961
set4 temp=654K nodes=80
set5 temp=653K nodes=89,90,651,652
set6 temp=646K nodes=646
ARRAY:
10 670.0
14 670.0
15 670.0
385 668.0
431 668.0
432 668.0
440 668.0
441 668.0
442 668.0
649 655.0
650 655.0
959 655.0
960 655.0
961 655.0
80 654.0
89 653.0
90 653.0
651 653.0
652 653.0
79 646.0
I hope the above is clear. Let me know if its not.
Thanks in advance.
Mohan
  댓글 수: 4
Image Analyst
Image Analyst 2013년 4월 2일
So the output is a cell array of strings:
ca{1} = 'set1 temp=670K nodes=10,14,15'
ca{2} = 'set2 temp=668K nodes=385,431,432,440,441,442'
ca{3} = 'set3 temp=655K nodes=649,650,959,960,961'
ca{4} = 'set4 temp=654K nodes=80'
ca{5} = 'set5 temp=653K nodes=89,90,651,652'
ca{6} = 'set6 temp=646K nodes=646'
or if you don't actually want to store those strings, then just print them out to the command line or a text file? Please define exactly what and where "outputs" means.
mohan
mohan 2013년 4월 3일
Hi
The exact outputs I require is:
OUTPUT 1: group the nodes to sets but only when there are more than 1 node. see below.
*Nset, nset=set1
10, 14, 15
*Nset, nset=set2
385, 431, 432, 440, 441, 442
*Nset, nset=set3
649, 650, 959, 960, 961
*Nset, nset=set4
89, 90, 651, 652
OUTPUT 2: now combine the above information and temperature data and single node values. see below.
set1, 670
set2, 668
set3, 655
80, 654
set4, 653
79, 646
I hope the above is clear. Please get back to me if not.
Thanks
Mohan

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

답변 (3개)

Image Analyst
Image Analyst 2013년 4월 1일
Did you try sortrows():
% Sort by column 2 in descending order.
sortedArray = sortrows(ARRAY, -2)

Brian B
Brian B 2013년 4월 1일
It depends how you want to store the sets. If you are comfortable with cell arrays, you can create one vector with the unique temperatures, and then create a cell array of the same size such that each cell has the set of nodes withe the corresponding temperature:
temperatures = unique(ARRAY(:,2));
nodes = arrayfun(@(temp)ARRAY(ARRAY(:,2)==temp, 1),temperatures,'UniformOutput',0)

Andrei Bobrov
Andrei Bobrov 2013년 4월 1일
a = [10 670.0
14 670.0
15 670.0
385 668.0
431 668.0
432 668.0
440 668.0
441 668.0
442 668.0
649 655.0
650 655.0
959 655.0
960 655.0
961 655.0
80 654.0
89 653.0
90 653.0
651 653.0
652 653.0
79 646.0];
[b, ~, c] = unique(a(:,2),'stable');
out = [num2cell((1:max(c2))') num2cell(b) accumarray(c2,a(:,1),[],@(x){x})];

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by