Cody: Problem 30 - function Sortrows

조회 수: 3 (최근 30일)
Marco Castelli
Marco Castelli 2012년 7월 27일
답변: Fatih Atilla PINAR 2020년 6월 9일
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

채택된 답변

Ryan
Ryan 2012년 7월 27일
편집: Ryan 2012년 7월 27일
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.
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2012년 7월 27일
+1. Note the . in front of the '
doc transpose %v.
doc ctranspose

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

추가 답변 (1개)

Fatih Atilla PINAR
Fatih Atilla PINAR 2020년 6월 9일
function zSorted = complexSort(z)
zSorted = sort(z,2,'descend');
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