Sorting an array in matlab
古いコメントを表示
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
2020 年 5 月 6 日
A similar question has already been answered here: https://www.mathworks.com/matlabcentral/answers/86608-how-to-sort-an-array-in-descending-order
Arif Istiak Abeg
2020 年 5 月 6 日
回答 (2 件)
Arif Istiak Abeg
2020 年 5 月 6 日
0 投票
Use the following code :
A=[12,8,15,6,26];
Ans = sort(A)
3 件のコメント
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
2020 年 5 月 6 日
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
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
2020 年 5 月 6 日
Arif Istiak Abeg
2020 年 5 月 6 日
use z=max(size(A))
instead of z=numel(A);
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!