필터 지우기
필터 지우기

how to make a sorting line by line?

조회 수: 2 (최근 30일)
sharifah shuthairah syed abdullah
i want to sort the value by line not sorting all number . below is my coding and example what i want
%%read data
clc
clear
%global f d;
%flow
f=[ 0 90 10 23 43 0 0 0 0 0 0 0
90 0 0 0 0 88 0 0 0 0 0 0
10 0 0 0 0 0 26 16 0 0 0 0
23 0 0 0 0 0 0 0 0 0 0 0
43 0 0 0 0 0 0 0 0 0 0 0
0 88 0 0 0 0 0 0 1 0 0 0
0 0 26 0 0 0 0 0 0 0 0 0
0 0 16 0 0 0 0 0 0 96 0 0
0 0 0 0 0 1 0 0 0 0 29 0
0 0 0 0 0 0 0 96 0 0 0 37
0 0 0 0 0 0 0 0 29 0 0 0
0 0 0 0 0 0 0 0 0 37 0 0];
%distance
d=[ 0 36 54 26 59 72 9 34 79 17 46 95
36 0 73 35 90 58 30 78 35 44 79 36
54 73 0 21 10 97 58 66 69 61 54 63
26 35 21 0 93 12 46 40 37 48 68 85
59 90 10 93 0 64 5 29 76 16 5 76
72 58 97 12 64 0 96 55 38 54 0 34
9 30 58 46 5 96 0 83 35 11 56 37
34 78 66 40 29 55 83 0 44 12 15 80
79 35 69 37 76 38 35 44 0 64 39 33
17 44 61 48 16 54 11 12 64 0 70 86
46 79 54 68 5 0 56 15 39 70 0 18
95 36 63 85 76 34 37 80 33 86 18 0];
[r,c]=size(f);
max_i=r;
max_j=r;
max_k=r;
max_q=r;
MaxIt=10;
cp=0.4; % Crossover Percentage
np=2*round(cp*r/2); % Number of Offsprings (Parents)
% % Create Empty Structure
empty_individual.P1=[];
empty_individual.P2=[];
% Create Population Matrix (Array)
pop=repmat(empty_individual,r,1);
for no=1:MaxIt
ID=randperm(r); %PERMUTATION r NOMBOR ..r = 3, 1 2 3, 1 3 2...
x=zeros(r,r);
n=length(ID);
x1=ID;
for i=1:n
for j=1:r
x(j,x1(j))=1;
B(no).mat=x; % keep memory when 0,1 permuation are make in (i),BINARY for RELATION
end
x=zeros(r,r);
end
z=0;xa=B(no).mat;
for i=1:max_i
for j=1:max_j
for k=1:max_k
for q=1:max_q
z= z+ f(i,k).*d(j,q).*xa(i,j).*xa(k,q);
end
end
end
end
no;
G(no,:)=[no z]; %STOR ALL VALUE Z 1 HINGGA MaxI IN MATRIX G
F(no,:)=[no z ID]; %STOR ALL VALUE Z and ID, 1 until MaxIt IN MATRIX F
end
%end
format compact % Suppresses the display of blank lines.
%
zmin=min(F(:,2));
zmax=max(F(:,2));
Iter_zvalue = F %matrix F have no iter, z and ID
% % Sort Population
%
RS = sort(F) % sorted from max to min
i want to sort F by line.. for example
before sorting
1 47764 7 9 4 5 2 1 11 10
2 48878 4 5 10 8 12 11 1 9
3 46388 9 6 4 2 1 10 3 8
4 32014 6 4 12 7 5 1 11 2
5 43468 11 3 8 9 4 10 6 12
6 36962 11 10 4 3 6 5 1 12
7 45400 4 1 10 12 7 11 5 8
8 39032 1 5 9 2 4 10 3 7
9 45742 4 2 5 11 9 3 6 1
10 37436 11 5 3 4 9 10 6 2
After sorting
1 32014 1 1 3 2 1 1 1 1
2 36962 4 2 4 2 2 1 1 2
3 37436 4 3 4 3 4 3 3 2
4 39032 4 4 4 4 4 5 3 7
5 43468 6 5 5 5 5 10 5 8
6 45400 7 5 8 7 6 10 6 8
7 45742 9 5 9 8 7 10 6 9
8 46388 11 6 10 9 9 10 6 10
9 47764 11 9 10 11 9 11 11 12
10 48878 11 10 12 12 12 11 11 12
what i want is that the value in bold just follow the however when i do the sort all the bold number also change.. how can i code so that the bold number did not change, just follow the big number in front.
  댓글 수: 2
madhan ravi
madhan ravi 2018년 11월 8일
honestly I don't see 1 32014 1 1 3 2 1 1 1 1
Iter_zvalue =
Columns 1 through 6
1 45270 10 6 1 8
2 49508 4 1 10 7
3 45194 10 4 9 11
4 45514 2 11 4 10
5 50604 4 3 5 7
6 31486 9 11 10 4
7 47882 3 9 11 1
8 48314 3 4 11 6
9 33334 10 5 9 12
10 35338 12 2 7 4
Columns 7 through 12
7 9 12 2 5 11
3 6 8 2 9 5
6 12 8 5 7 3
5 8 6 1 12 3
11 2 9 6 10 8
6 12 8 1 2 5
7 12 2 6 5 8
2 12 1 8 10 9
1 11 2 4 3 7
10 9 5 6 8 11
Columns 13 through 14
3 4
11 12
2 1
9 7
12 1
7 3
4 10
7 5
8 6
3 1
RS =
Columns 1 through 6
1 31486 2 1 1 1
2 33334 3 2 4 4
3 35338 3 3 5 4
4 45194 4 4 7 6
5 45270 4 4 9 7
6 45514 9 5 9 7
7 47882 10 6 10 8
8 48314 10 9 10 10
9 49508 10 11 11 11
10 50604 12 11 11 12
Columns 7 through 12
1 2 1 1 2 3
2 6 2 1 3 3
3 8 2 2 5 5
5 9 5 2 5 5
6 9 6 4 7 7
6 11 8 5 8 8
7 12 8 6 9 8
7 12 8 6 10 9
10 12 9 6 10 11
11 12 12 8 12 11
Columns 13 through 14
2 1
3 1
3 1
4 3
7 4
7 5
8 6
9 7
11 10
12 12
>>
sharifah shuthairah syed abdullah
you wont see the same answer as mine if you run the code.. because it will generate different answer every time we run the coding
i want the answer for to be like this
for example
Iter_zvalue =
1 45270 *10 6 1 8*
2 49508 *4 1 10 7*
3 45194 *10 4 9 11*
4 45514 *2 11 4 10*
5 50604 *4 3 5 7*
6 31486 *9 11 10 4*
7 47882 *3 9 11 1*
8 48314 *3 4 11 6*
9 33334 *10 5 9 12*
10 35338 *12 2 7 4*
the answer should be like this
RS=
1 31486 *9 11 10 4*
2 33334 *10 5 9 12*
3 35338 *12 2 7 4*
4 45194 * 10 4 9 11*
5 45270 *10 6 1 8*
6 45514 *9 5 9 7*
7 47882 *3 9 11 1*
8 48314 *3 4 11 6*
9 49508 *4 1 10 7*
10 50604 *4 3 5 7*
the number in the * for RS must be the same as the number in the * for Iter_zvalue .. however it when running the code it also sort

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

채택된 답변

Stephen23
Stephen23 2018년 11월 8일
편집: Stephen23 2018년 11월 8일
Here are two methods that match your requested output. Your example input data:
>> M = [1,45270,10,6,1,8;2,49508,4,1,10,7;3,45194,10,4,9,11;4,45514,2,11,4,10;5,50604,4,3,5,7;6,31486,9,11,10,4;7,47882,3,9,11,1;8,48314,3,4,11,6;9,33334,10,5,9,12;10,35338,12,2,7,4]
M =
1 45270 10 6 1 8
2 49508 4 1 10 7
3 45194 10 4 9 11
4 45514 2 11 4 10
5 50604 4 3 5 7
6 31486 9 11 10 4
7 47882 3 9 11 1
8 48314 3 4 11 6
9 33334 10 5 9 12
10 35338 12 2 7 4
Method one: sortrows:
>> Z = sortrows(M,2);
>> Z(:,1) = 1:size(Z,1)
Z =
1 31486 9 11 10 4
2 33334 10 5 9 12
3 35338 12 2 7 4
4 45194 10 4 9 11
5 45270 10 6 1 8
6 45514 2 11 4 10
7 47882 3 9 11 1
8 48314 3 4 11 6
9 49508 4 1 10 7
10 50604 4 3 5 7
Method two: sort and indexing:
>> [Z,idx] = sort(M(:,2),1);
>> Z = [M(:,1),Z,M(idx,3:end)]
Z =
1 31486 9 11 10 4
2 33334 10 5 9 12
3 35338 12 2 7 4
4 45194 10 4 9 11
5 45270 10 6 1 8
6 45514 2 11 4 10
7 47882 3 9 11 1
8 48314 3 4 11 6
9 49508 4 1 10 7
10 50604 4 3 5 7
  댓글 수: 1
sharifah shuthairah syed abdullah
thank you so much... your methods really help me.. thank you so much

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

추가 답변 (0개)

카테고리

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