MATLAB Answers

Sorting an array of numbers in descending order by using while loop

조회 수: 44(최근 30일)
Syd Shahed
Syd Shahed 4 May 2020
댓글: Guillaume 4 May 2020
I wrote the code to sort number in descending order but my code failed to do so (actually i was the failure lol).
Example: Consider, the given array is x=[4 6 9 2 5 0 1] and i want the output y as [9 6 5 4 2 1 0].
What is more, my thinking was like that in seq-
1) Find maximum number from a given array
2) Then use find function to detect the maximum element idexing
3) And then remove the maximum number from the array and reducing the array length
However, i am successfully failed to do what i am looking for. I am determined to use only the while loop to execute this code (nothing else is appreciable e.g sort function and for loop)
Can anyone tell me where i did wrong while i was writing the code? i mean the erroneous part of the code.
I will be glad if any expert give me an advice or suggestion to correct the the code.
function y =sortingnumsindes(x)
y=[];
while(numel(x)>0)
y=max(x);
p=find(x==y);
x(p)=[];
end
y=x;
end

  댓글 수: 0

Sign in to comment.

채택된 답변

David Hill
David Hill 4 May 2020
function y =sortingnumsindes(x)
y=[];
while(numel(x)>0)
m=max(x);
p=find(x==m,1);%need ,1 to handle any duplicates
x(p)=[];
y=[y,m];%needed to store each subsequent element in array y
end
end

  댓글 수: 6

표시 이전 댓글 수: 3
Steven Lord
Steven Lord 4 May 2020
Guillaume, Syd Shahed actually posted most of that code already. I personally might have been a bit more vague, simply pointing out that the variable y was being overwritten each iteration through the while loop and the maximum value was being thrown away. I likely also would have suggested calling max with two outputs.
But this doesn't feel to me like other situations where someone posts a complete solution to a problem where the original poster didn't show any work.
Syd Shahed
Syd Shahed 4 May 2020
Thanks brother , you get the point . Actually i am a newbie and sometimes face some awkward problems thats why need a clear view to understand what happening inside . And David doing really well .
Guillaume
Guillaume 4 May 2020
As my comment seems to be a bit controversial here is some additional reason why I initially wrote it:
There are several steps to writing code. There's first figuring out that algorithm, already there's a problem with the algorithm written, it's missing a step where the maximum is appended to the output. Then there's translating that algorithm into code, and here there was several issue. Then there's the debugging part and understanding where it's gone wrong. In my opinion, in order to get better at coding you need to go through all these steps yourself. Here in particular, Syd needed to understand that y was used for two different things and that he actually never went through the missing step of storing the elements that are removed.
So, I'm afraid the "clear view to understand what happening inside" can only come through a process of trial and error, not by having the solution handed directly to you.

Sign in to comment.

추가 답변(1개)

Guillaume
Guillaume 4 May 2020
"1) Find maximum number from a given array"
"2) Then use find function to detect the maximum element idexing"
Have a look at the documentation of max, in particular the 2nd output which is exactly the index you want.
p=find(x==y);
will work in your code as long as there's only one value equal to the max. If there's more than one, p is a vector of indices and you'll end up deleting too many elements.
y=[];
%...
y=max(x);
%...
y=x;
You need to rethink what y is, you're using it for two purpose, and how you fill it.

  댓글 수: 1

Syd Shahed
Syd Shahed 4 May 2020
I am puzzled bro . Its still shows the last number in the sequence. Cant find a way out of here ?

Sign in to comment.


Translated by