Sorting an array in matlab

21 Ansichten (letzte 30 Tage)
Ghanem Abbara
Ghanem Abbara am 6 Mai 2020
Kommentiert: Guillaume am 6 Mai 2020
Hello all
A= [12,8,15,6,26]
I have this array and i want to sort it descending
Can you write the code for this qeustion by using for and if
THANK YOU
  2 Kommentare
Happy Bear
Happy Bear am 6 Mai 2020
Arif Istiak Abeg
Arif Istiak Abeg am 6 Mai 2020
B = sort(___,direction) returns sorted elements of A in the order specified by direction using any of the previous syntaxes. 'ascend' indicates ascending order (the default) and 'descend' indicates descending order.
use Ans=sort(A,descend);

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Arif Istiak Abeg
Arif Istiak Abeg am 6 Mai 2020
Use the following code :
A=[12,8,15,6,26];
Ans = sort(A)
  3 Kommentare
Ghanem Abbara
Ghanem Abbara am 6 Mai 2020
ı want ıt by using for and ıf
Guillaume
Guillaume am 6 Mai 2020
See how do I get help on homework question. Don't expect us to do your homework for you if you show no effort.

Melden Sie sich an, um zu kommentieren.


Arif Istiak Abeg
Arif Istiak Abeg am 6 Mai 2020
Hope it will work :
A= [12,8,15,6,26];
z=numel(A);
for c=1:z
for k=c+1:z
if A(c)<A(k)
x=A(c );
A( c) =A(k);
A(k)=x;
end
end
end
  3 Kommentare
Arif Istiak Abeg
Arif Istiak Abeg am 6 Mai 2020
use z=max(size(A))
instead of z=numel(A);
Guillaume
Guillaume am 6 Mai 2020
For a vector, max(size(A)) and numel(A) are identical. For arrays of any size, max(size(A)) is length(A).
I would recommend that you let the OP figure out the solution to their homework themselves.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by