命令A(〜A)在matlab中实际上是做什么的 [英] What does the command A(~A) really do in matlab
问题描述
我一直在寻找找到矩阵非零极小值的最有效方法,并在论坛上找到它:
I was looking to find the most efficient way to find the non zero minimum of a matrix and found this on a forum :
让数据成为矩阵A
.
A(~A) = nan;
minNonZero = min(A);
这是非常简短和有效的(至少在代码行数上),但是我不明白在执行此操作时会发生什么.我找不到关于此的任何文档,因为它不是像+
,-
,\
这样的矩阵上的operation
.
This is very short and efficient (at least in number of code lines) but I don't understand what happens when we do this. I can't find any documentation about this since it's not an operation
on matrices like +
,-
,\
,... would be.
有人可以给我解释一下还是给我链接,或者可以帮助我了解完成的事情吗? 谢谢 !
Could anyone explain me or give me a link or something that could help me understand what is done ? Thank you !
推荐答案
它使用 ~
是not运算符.当在双精度数组上使用时,它将查找所有等于零的元素.例如:
~
in Matlab is the not operator. When used on a double array, it finds all elements equal to zero. e.g.:
~[0 3 4 0]
逻辑矩阵中的结果
[1 0 0 1]
即这是找到所有零元素的快速方法
i.e. it's a quick way to find all the zero elements
因此,如果A
= [0 3 4 0]
,则~A
= [1 0 0 1]
,因此现在A(~A)
= A([1 0 0 1])
. A([1 0 0 1])
使用逻辑索引仅影响正确的元素,因此在本例中为元素1和元素4.
So if A
= [0 3 4 0]
then ~A
= [1 0 0 1]
so now A(~A)
= A([1 0 0 1])
. A([1 0 0 1])
uses logical indexing to only affect the elements that are true so in this case element 1 and element 4.
最后,A(~A) = NaN
将用NaN
替换A中等于0
的所有元素,而min
忽略该元素,从而找到最小的非零元素.
Finally A(~A) = NaN
will replace all the elements in A that were equal to 0
with NaN
which min
ignores and thus you find the smallest non-zero element.
这篇关于命令A(〜A)在matlab中实际上是做什么的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!