Borrar filtros
Borrar filtros

How to sort array data without using sort function?

2 visualizaciones (últimos 30 días)
jeff417
jeff417 el 14 de Abr. de 2016
Respondida: Walter Roberson el 14 de Abr. de 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

Respuestas (1)

Walter Roberson
Walter Roberson el 14 de Abr. de 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

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by