How to sort array data without using sort function?

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by