misbehaving of sort function, for looping the first and last rows not orienting to my expected results and well as specified indexes not corrects

Hi misbehaving of sort function, for looping the first and last rows not orienting to my expected results and well as specified indexes not corrects. Any assistance will be appreciated too. see the code:
>> a=[7 9 5 7; 1 5 8 9; 1 2 7 3;10 0 9 9]
[A B]=sort(a)
a =
7 9 5 7
1 5 8 9
1 2 7 3
10 0 9 9
A =
1 0 5 3
1 2 7 7
7 5 8 9
10 9 9 9
B =
2 4 1 3
3 3 3 1
1 2 2 2
4 1 4 4
>> [sizr sizc]=size(a);
aaa=zeros(sizr,sizc);
for i=1:4
if(A(B(i,3),3)>A(B(i,2),2)), aaa(B(i,3),:)=a(B(i,3),:), aA=A(B(i,3),3)
else, aaa(B(i,2),:)=a(B(i,2),:), aA=A(B(i,2),2)
end, end
aaa =
0 0 0 0
0 0 0 0
0 0 0 0
10 0 9 9
aA = 9
aaa =
0 0 0 0
0 0 0 0
1 2 7 3
10 0 9 9
aA = 8
aaa =
0 0 0 0
1 5 8 9
1 2 7 3
10 0 9 9
aA = 7
aaa =
0 0 0 0
1 5 8 9
1 2 7 3
10 0 9 9
aA = 9

6 commentaires

sort is doing exactly what you are telling it to do in case of a matrix: sorting each column separately.
Also, have a read here and here. It will greatly improve your chances of getting an answer.
@Fredius Magige: today I formatted your code for you. In future you can do it yourself by selecting the code text and clicking the {} Code button above the textbox.
"misbehaving of sort function"
Nope, it is behaving exactly like the documentation states. Did you read it?
Please observe the answers as looping as: aaa-some rows not correct first looping last row, and last looping first row aA all results not correct Please assist me on that as my code above
So the problem is not with the output of sort? What is the goal of this code? You may be better off posting in your own language.
As far as I can make out, you are expecting the sorting to occur along the rows, but the ‘sort’ function sorts along the columns unless you specify otherwise.
sort(a,1) sorts along each column
sort(a,2) sorts along each row
sort(a) by default sorts along each column

Connectez-vous pour commenter.

 Réponse acceptée

Hi all my collegues, I got best answer by manipulation of sort as:
[AA BB]=sort(Graptable(:,[12:21,1:11])); Graptable = Graptable (BB,[1:21]);
Graptable(2*JmRow+1:end,:)=[ ]; Graptable (:,22)= 0;
for ii=1:JmRow
if(Graptable(ii,12)~=0&&Graptable(ii,13)~=0), Graptable (ii,:)=0;
else, end, end
Graptable (JmRow+1:end,22)=Graptable(JmRow+1:end,12);
Graptable (1:JmRow,22)=Graptable(1:JmRow,13);
SOORTING1=sortrows(Graptable,[22,12]); SOORTING1(1:JmRow,:)=[];%SOORTING1(:,22)=[ ];
Thanks MatLab community too

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by