how can I create a vector with unknown length?

12 ビュー (過去 30 日間)
sapir
sapir 2013 年 5 月 9 日
I need to create a user defined function that recieves a vector x of any length and than sorts the vector from the largest to the smallest into a new vector y. I'm not allowed to use the function sort. I have no idea how to begin! please help me!! thank u!!!

回答 (3 件)

Roger Stafford
Roger Stafford 2013 年 5 月 9 日
This article should be of help to you.
https://en.wikipedia.org/wiki/Sorting_algorithm
I tend to prefer the merge sort algorithm.

John Doe
John Doe 2013 年 5 月 9 日
編集済み: John Doe 2013 年 5 月 9 日
The most intuitive (and simple to implement) is probably bubble sort:
Since this is an assignment, I'll leave some of it open, so fill in the blanks =)
n=length(A);
for j=1:1:n-1
% comparing each number with the next and swapping
for i=1:1:n-1
if ...
% In here you should check if A(i) > A(i+1)
% If so, switch places. You need a temporary
% variable, in order to not overwrite something.
end
end
end
Good luck =)

Sean de Wolski
Sean de Wolski 2013 年 5 月 9 日
xsorted = sortrows(x(:),-1)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by