フィルターのクリア

How can i find the sorted indexing of the array

1 回表示 (過去 30 日間)
Maheen Fazal
Maheen Fazal 2019 年 5 月 6 日
コメント済み: Maheen Fazal 2019 年 5 月 9 日
Hi, Sir how can i find the sorted indexing of the array when i am sorting it in the same code??
A = [2 9 6 5 8]
swapped = 1;
while swapped
swapped = 0;
for i=1:numel(A)-1
if A(i+1) > A(i)
memory = A(i);
A(i) = A(i+1);
A(i+1) = memory;
swapped = 1;
end
end
end
disp(A)

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 5 月 6 日
A = [2 9 6 5 8];
n = numel(A);
AA = [A;1:n];
swapped = 1;
while swapped
swapped = 0;
for ii = 1:n-1
if AA(1,ii+1) > AA(1,ii)
memory = AA(:,ii);
AA(:,ii) = AA(:,ii+1);
AA(:,ii+1) = memory;
swapped = 1;
end
end
end
Asort = AA(1,:);
Index_sort = AA(2,:);
  1 件のコメント
Maheen Fazal
Maheen Fazal 2019 年 5 月 9 日
bundle of thanks Sir

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by