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

Happy Bear
Happy Bear 2020년 5월 6일
Arif Istiak Abeg
Arif Istiak Abeg 2020년 5월 6일
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);

댓글을 달려면 로그인하십시오.

답변 (2개)

Arif Istiak Abeg
Arif Istiak Abeg 2020년 5월 6일

0 개 추천

Use the following code :
A=[12,8,15,6,26];
Ans = sort(A)

댓글 수: 3

Guillaume
Guillaume 2020년 5월 6일
Note that it is not going to sort the array descending as asked. A small tweak to this answer would do it, the OP can find what it is by looking up the documentation of sort.
Of course, I suspect that this is a homework question and that the OP is supposed to use a loop, not the sort function.
Ghanem Abbara
Ghanem Abbara 2020년 5월 6일
ı want ıt by using for and ıf
Guillaume
Guillaume 2020년 5월 6일
See how do I get help on homework question. Don't expect us to do your homework for you if you show no effort.

댓글을 달려면 로그인하십시오.

Arif Istiak Abeg
Arif Istiak Abeg 2020년 5월 6일

0 개 추천

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

Ghanem Abbara
Ghanem Abbara 2020년 5월 6일
Not Working ! :(
Arif Istiak Abeg
Arif Istiak Abeg 2020년 5월 6일
use z=max(size(A))
instead of z=numel(A);
Guillaume
Guillaume 2020년 5월 6일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2020년 5월 6일

댓글:

2020년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by