Cody: Problem 30 - function Sortrows
2 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
2012 年 7 月 27 日
+1. Note the . in front of the '
doc transpose %v.
doc ctranspose
その他の回答 (1 件)
Fatih Atilla PINAR
2020 年 6 月 9 日
function zSorted = complexSort(z)
zSorted = sort(z,2,'descend');
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!