sort an array without using the command sort

function sortvector(v)
v1=zeros(1,length(v));
for i=1:length(v)
v1(i)=min(v);
v(find(v==v1(i)))=[];
end
disp(v1)
end
the question was to sort an array without using the command sort. My method was to extract the minimums one by one and put blanks into the elements extracted. However this did not work. What's wrong with this function?
Correction of this method and other methods would help. Thanks.

 채택된 답변

James Tursa
James Tursa 2020년 11월 4일

0 개 추천

Instead of using find, you could just have the min( ) function return the index to you. And then delete that element.
[v1(i),k] = min(v);
v(k) = [];

댓글 수: 2

SENLIN YUE
SENLIN YUE 2020년 11월 4일
Thanks! It worked, but why using find is wrong?
find might return multiple indexes.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2020년 11월 4일

댓글:

2020년 11월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by