Alternative to the sort function
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Charles Perkins
am 3 Mai 2022
Kommentiert: Steven Lord
am 7 Mai 2022
Looking for an alternative way to sort values in either decending or acsending order without using the sort function
2 Kommentare
Walter Roberson
am 3 Mai 2022
Why? Your reason for not using sort() will influence the recommendations.
Akzeptierte Antwort
Walter Roberson
am 3 Mai 2022
https://www.mathworks.com/matlabcentral/answers/49226-how-to-apply-a-mean-filter-for-3x3#comment_2111490 and look for bogosort() source code there.
You would have to adapt it slightly to give a choice of ascending or descending, and you would have to adapt for non-vectors.
7 Kommentare
Steven Lord
am 7 Mai 2022
One suggestion that will eliminate the need for your count variable: the end function when used as an index refers to the position of the last element / row / column / etc. in an array. You can use this in assignment or referencing statements, including using it in some simple mathematical expressions. Let's start off with an empty array x.
x = []
We can assign to an element one past the end to make the vector longer:
x(end+1) = 1
We can do that again.
x(end+1) = 3
Let's reset the last value.
x(end) = 42
In this next statement, x has 2 elements so end+2 is 4. Therefore the next command sets x(4) to 10 with x(3) filling in with the default value of 0.
x(end+2) = 10
We don't have to add to end, we could also subtract. x has 4 elements so end-3 is 1 and y is x(1).
y = x(end-3)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!