필터 지우기
필터 지우기

How to sort array data without using sort function?

조회 수: 2 (최근 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 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