## Cody: Problem 30 - function Sortrows

Asked by Marco Castelli

### Marco Castelli (view profile)

on 27 Jul 2012
Accepted Answer by Ryan

### Ryan (view profile)

Hi,
i'm "solving" number 30 cody's problem.
I think to solve that whit sortrows function.
If I have a z vector:
j = sqrt(-1);
z = [-4 6 3+4*j 1+j 0];
my funtion is:
function z = complexSort(z)
z(2,:)=sqrt(real(z).^2+imag(z).^2);
z=sortrows(z',-2);
z=z(:,1);
end
End it return the result
z =
6.0000 6.0000
3.0000 - 4.0000i 5.0000
-4.0000 4.0000
1.0000 - 1.0000i 1.4142
0 0
The question is: why imagine part in input is positive e sortrows trasform it in negative?
best regards
Marco

on 27 Jul 2012
Edited by Ryan

### Ryan (view profile)

on 27 Jul 2012

You were taking the complex conjugate of z. Be careful whenever you use ' to transpose in Matlab!
function z = complexSort(z)
z(2,:)=sqrt(real(z).^2+imag(z).^2);
z=sortrows(z.',-2);
z=z(:,1);
end
That should work for you.

Sean de Wolski

### Sean de Wolski (view profile)

on 27 Jul 2012
+1. Note the . in front of the '
doc transpose %v.
doc ctranspose