How to sort array data without using sort function?

6 ビュー (過去 30 日間)
jeff417
jeff417 2016 年 4 月 14 日
回答済み: Walter Roberson 2016 年 4 月 14 日
I have to sort my data from either lowest to highest or highest to lowest. My code is working, but how can I set the output to a variable from within the function? Right now it just outputs it as ans or AA, but I eventually want to edit the function to run for more than one data set, and I can't figure out how to set the answer to a variable.
function [AA] = Project2_3_eu6456_fr5126_fx9640(A)
%Sort Data in File
z = length(A);
B = input('Enter 1 to sort the data from highest to lowest: \nEnter 2 to sort the data from lowest to highest: ');
if B == 2
for i = 1:z
[C D] = min(A);
A(D) = [];
AA(i) = C;
end
elseif B == 1
for j = 1:z
[G H] = max(A);
A(H) = [];
AA(j) = G;
end
else
display('Error, enter 1 or 2.')
end
end

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 4 月 14 日
Don't do that. Assign the output to a variable when you call it. For example,
for K = 1 : 5
Output{K} = Project2_3_eu6456_fr5126_fx9640(rand(3, 8));
end

カテゴリ

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