Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Help with storing values for the loop I created want to store them with where they are located on the vector

1 回表示 (過去 30 日間)
Yogesh Bhambhwani
Yogesh Bhambhwani 2020 年 9 月 24 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Help with storing values for the loop I created want to store them with where they are located on the vector
the vector is [4 1 6 9 2 1 8 3]
want to sort from smallest to biggest which I did, but I can not seem to figure out how to store them from where they were found on the vector.

回答 (2 件)

Dana
Dana 2020 年 9 月 24 日
編集済み: Dana 2020 年 9 月 24 日
Is this what you're after?
vector = [4 1 6 9 2 1 8 3];
[sorted,sortinds] = sort(vector)
sorted =
1 1 2 3 4 6 8 9
sortinds =
2 6 5 8 1 3 7 4
Here, sortinds gives the locations of the sorted values in the original vector vector, e.g., the first element in the sorted vector is 1=vector(2), the second is 1=vector(6), the third is 2=vector(5), etc.
  5 件のコメント
Yogesh Bhambhwani
Yogesh Bhambhwani 2020 年 9 月 27 日
The loop goes over for if statements to find the smallest value in the vector then I have to create another for if statement that stores the value from the for if statement from before and repeats that.
Dana
Dana 2020 年 9 月 28 日
Your function takes an input vector, and returns two outputs, (i) sorted and (ii) indices. As I understand it, those two outputs should be (i) sorted = the elements of vector sorted into ascending order, and (ii) indices = the locations in vector of each element in sorted.
Is that understanding correct? If so, then
[sorted,indices] = sort(vector)
does exactly what you need. There would be no need to loop on anything.
On the other hand, if I'm misunderstanding things, please try to clearly explain what the function arguments are supposed to equal. In doing so, forget for a minute about how to get those outputs (e.g., don't tell me about the algorithm you think should be used), just explain what those output should be in the end.

madhan ravi
madhan ravi 2020 年 9 月 26 日
Google bubblesort.
  2 件のコメント
Yogesh Bhambhwani
Yogesh Bhambhwani 2020 年 9 月 27 日
I don’t need to sort them I already did that I just to store the where they found the value in the array repeatedly.
madhan ravi
madhan ravi 2020 年 9 月 28 日
Loop iterator tells you exactly where it was found. It was your homework to use loops, so read about the algorithm.

Community Treasure Hunt

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

Start Hunting!

Translated by