Azzera filtri
Azzera filtri

How to sort array data without using sort function?

2 visualizzazioni (ultimi 30 giorni)
jeff417
jeff417 il 14 Apr 2016
Risposto: Walter Roberson il 14 Apr 2016
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

Risposte (1)

Walter Roberson
Walter Roberson il 14 Apr 2016
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

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by