开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜