I need to sort scalars. Is that possible?
Show older comments
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?
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 12 Jun 2015
x=[1 2 3 0 5 10 ]
x1=sort(diff(x))
3 Comments
Prakriti Sardana
on 12 Jun 2015
Stephen23
on 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
on 12 Jun 2015
Edited: Prakriti Sardana
on 12 Jun 2015
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!