get magnitude of values of a matrix and sort them in a descend way MATLAB
How to get the magnitude of each value in a matrix, so:
0.2964 0.8765 0.3793
0.6832 -0.4721 0.5571
-0.6674 -0.0941 0.7387
is transformed?
0.2964 0.8765 0.3793
0.6832 0.4721 0.5571
0.6674 0.0941 0.7387
to sort the in a descend way, we do sort(A,'descend'), but as I have negative values I would like to have the magnitudes and then sort, can this be done in a single instruction??(get magnitudes and sort them in descending order)
so at the end we get
0.8765
0.7387
0.6832
0.6674
0.5571
0.3793 开发者_如何学JAVA
0.4721
0.2964
0.0941
Try sort(abs(A(:)),'descend')
What about this:
>> a
a =
0.2964 0.8765 0.3793
0.6832 -0.4721 0.5571
-0.6674 -0.0941 0.7387
>> temp=sort(abs(a(:)),'descend')
ans =
0.8765
0.7387
0.6832
0.6674
0.5571
0.4721
0.3793
0.2964
0.0941
Use SORT and ABS:
>> x = [0.2964 0.8765 0.3793; 0.6832 -0.4721 0.5571; -0.6674 -0.0941 0.7387]
x =
0.2964 0.8765 0.3793
0.6832 -0.4721 0.5571
-0.6674 -0.0941 0.7387
>> sort(abs(x(:)), 'descend')
ans =
0.8765
0.7387
0.6832
0.6674
0.5571
0.4721
0.3793
0.2964
0.0941
Use the abs() function.
加载中,请稍侯......
精彩评论