Lets say I have 2 matrices,
a = [2,5,3,6]
b = [9,1,8,4]
What I want to do is create a function that sorts A from lowest to highest, and the corresponding values of B hold. I need to do this without using sortrows, however.
Here's my idea at the moment.
function [ out ] = mergesort( a,b )
c = [a;b]
d = mergesort(c,:))
out = [c;b]
Now the ending is where I have a bit of trouble, as the first array is sorted but the second is (obviously) not. Can anyone help me please?
EDIT: Here is my merge sort algorithm.
function [ out ] = mergesort (a)
n=length(a);
if (n==1)
out=a; return
end
out=merge(mergesort(a(1,1:floor(n/2))),...
mergesort(a(1,floor(n/2)+1:n)));
end
EDIT 2: Here is the 'merge' algorithm.
function [ out ] = merge(a,b)
lena = length(a);
lenb = length(b)
out=zeros(1,lena+lenb);
j=1;
k=1;
l=1;
while (j<=lena)&&(k<=lenb))
if (a(1,j)<b(1,k))
out(1,l) = a(1,j); j=j+1; l=l+1;
else
out(1,l) = b(1,k); k=k+1, l=l+1;
end
end
while (j<=lena)
out(1,l) = a(1,j); j=j+1; l=l+1;
end
while (k<=lenb)
out(1,l) = b(1,k); k=k+1;l=l+1;
end
end