What command does the same as find but including null elements?

So this exercise asks you order any vector in a manner such that the minor elements are on the front and the major elements on the back.
Its pseudo code goes like this:
Ordering of a vector (x1, x2, . . . , xn) by selection
for i ranging from 1 to n-1
k ← i
for j ranging from i + 1 to n
if xj < xk
then k ← j
end of cycle in j
t ← xi
xi ← xk
xk ← t
end on cycle in i
Then you are asked to create a code that does the same in Matlab. Here's what i've got for starters:
v = input( 'vector? ' )
for v(1:end)
k = find(v)
My problem is that find ignores the elements that are equal to zero. That would screw up my ordering. So what command does the same as find while taking the zeros into consideration?

2 件のコメント

madhan ravi
madhan ravi 2018 年 12 月 21 日
what's the input and your desired result?
bobsoney bobsoney
bobsoney bobsoney 2018 年 12 月 21 日
any vector at all. The desired result is the same vector ordered from minor to major elements.
for example:
vector? v = [1 8 4 3 6 6 2 4]
it would then return
v = [1 2 3 4 4 6 6 8]

サインインしてコメントする。

 採用された回答

madhan ravi
madhan ravi 2018 年 12 月 21 日
編集済み: madhan ravi 2018 年 12 月 21 日

0 投票

v = [1 8 4 3 6 6 2 4];
desired_result=sort(v)
Gives:
desired_result =
1 2 3 4 4 6 6 8

3 件のコメント

bobsoney bobsoney
bobsoney bobsoney 2018 年 12 月 21 日
Oh thanks thats great. The problem is that this is specifically made for showing off.
We're supposed to do it the hard way. I'd have to create a code that would manually go through each element, putting the smallest element before the bigger one.
bobsoney bobsoney
bobsoney bobsoney 2018 年 12 月 21 日
Jesus that is pretty fuckin complex. Thanks though lol. Gonna try and figure it out.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by