Sorting Algorithm

조회 수: 3 (최근 30일)
Buddy Britton
Buddy Britton 2011년 5월 4일
I need to take two arrays and sort one in ascending order, while carrying along the second one. I cannot use the sort function in Matlab.
ex) Array1 = [3,-1,-34,10,8] Array2 = [2,-9,4,-5,0]

답변 (4개)

Sean de Wolski
Sean de Wolski 2011년 5월 4일
Then even better - trump your teacher with sortrows!
A = sortrows([array1(:),array2(:)]);
array1_sorted = A(:,1).';
array2_sorted = A(:,2).';

Andrei Bobrov
Andrei Bobrov 2011년 5월 5일
more variant
Array1 = [3,-1,-34,10,8];
Array2 = [2,-9,4,-5,0];
A = [Array1;Array2];
SortedArray = [];
while ~isempty(A)
[~,I] = min(A(1,:));
A(:,[1 I]) = A(:,[I 1]);
SortedArray = [SortedArray A(:,1)];
A = A(:,2:end);
end
or
Array1 = [3,-1,-34,10,8];
Array2 = [2,-9,4,-5,0];
A = [Array1;Array2];
k = 1:length(A);
while ~isempty(k)
[~,I] = min(A(1,k));
A(:,[k(1) k(I)]) = A(:,[k(I) k(1)]);
k = k(2:end);
end
or
Array1 = [3,-1,-34,10,8];
Array2 = [2,-9,4,-5,0];
A = [Array1;Array2];
n = length(A);
for j = 1:n
k = j:n;
[~,I] = min(A(1,k));
A(:,[j k(I)]) = A(:,[k(I) j]);
end
condition without built-in sort functions. I think sortrows, sort and unique - sort function MATLAB

Matt Tearle
Matt Tearle 2011년 5월 4일
In the spirit of Sean's answer... this works for the given example:
[~,idx] = unique(Array1);
Array2(idx)
EDIT (and may Cleve have mercy on my soul)
[s1,idx] = unique(Array1);
if numel(s1)<numel(Array1)
s2 = [];
for k=1:numel(s1)
s2 = [s2,Array2(s1(k)==Array1)];
end
else
s2 = Array2(idx);
end
s2
  댓글 수: 7
Buddy Britton
Buddy Britton 2011년 5월 4일
Matt, I tried running your edited version and there is an error in line 1 "[s1,idx] = unique(Array1)"
Matt Tearle
Matt Tearle 2011년 5월 5일
a) Any particular error? Given that it works fine for me (R2011a), it's a bit hard for me to tell why you might be getting "an error".
b) I don't know why you can't use the sort function, but, whatever the reason, I doubt you can really use the unique function either. (Or sortrows)
b, part 2) Sean and I gave answers that technically didn't use sort, but are not good ways to solve the problem (unless there really is a reason you can use unique or sortrows, but not sort).

댓글을 달려면 로그인하십시오.


Daniel Shub
Daniel Shub 2011년 5월 5일
I think the sortrows/unique answers may be cheating since they might call sort under the hood ... I am sure this student would not want to cheat. My solution uses both eval and feval, so should get extra credit.
Array1 = [3,-1,-34,10,8];
Array2 = [2,-9,4,-5,0];
while 42 > sqrt(pi)
eval('x = randperm(length(Array1));');
if feval('all', diff(Array1(x)) > 0)
return;
else
disp('MATLAB is stupid and guessed wrong!');
disp(['[', num2str(Array1(x)), '] is not sorted!']);
disp('Trying again!');
disp(' ');
end
end
Array1(x)
Array2(x)
Any concerns about inefficiencies are silly as the distributed computing toolbox would allow my answer to harness the power of a computer cluster.

카테고리

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