I need to sort scalars. Is that possible?
Ältere Kommentare anzeigen
My matlab code for a simple program is:
for t=1:length(x)
x1 = x(t+1) - x(t)
end
Now, I need to sort the variable 'x1', which is a scalar, in ascending order. Is that possible?
Or, can I convert my scalar 'x1' into a vector?
Akzeptierte Antwort
Weitere Antworten (1)
Azzi Abdelmalek
am 12 Jun. 2015
x=[1 2 3 0 5 10 ]
x1=sort(diff(x))
3 Kommentare
Prakriti Sardana
am 12 Jun. 2015
Stephen23
am 12 Jun. 2015
@Prakriti Sardana: you are correct, it does not work for a loop. Because MATLAB is a high-level language, and often doing basic things in a loop is the slow and awkward way of doing things. To use MATLAB efficiently you need to learn about code vectorization, which is what Azzi Abdelmalek is doing. This is much faster, simpler and less buggy than using loops.
Prakriti Sardana
am 12 Jun. 2015
Bearbeitet: Prakriti Sardana
am 12 Jun. 2015
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!