Answers

Question and Answer:

  Home  Wipro

⟩ How to sort array elements in ascending or descending order?

Integer, floating-point, logical, and character arrays are permitted. Floating-point arrays can be complex. For elements of A with identical values, the order of these elements is preserved in the sorted list. When A is complex, the elements are sorted by magnitude, i.e., abs(A), and where magnitudes are equal, further sorted by phase angle, i.e., angle(A), on the interval [−π, π]. If A includes any NaN elements, sort places these at the high end.

B = sort(A,dim) sorts the elements along the dimension of A specified by a scalar dim.

B = sort(...,mode) sorts the elements in the specified direction, depending on the value of mode.

'ascend'

Ascending order (default)

'descend'

Descending order

[B,IX] = sort(A,...) also returns an array of indices IX, where size(IX) == size(A). If A is a vector, B = A(IX). If A is an m-by-n matrix, then each column of IX is a permutation vector of the corresponding column of A, such that

for j = 1:n

B(:,j) = A(IX(:,j),j);

end

If A has repeated elements of equal value, the returned indices preserve the original ordering.

Example:Sort horizontal vector A:

A = [78 23 10 100 45 5 6];

sort(A)

ans =5 6 10 23 45 78 100

 179 views

More Questions for you: