[英]MATLAB logical indexing: Is a(find(b<1)) = 0, the same as a(b(b<1)) = 0?
As my question suggests, my goal is to find all the indices of the values in b less than 1, and set these same indices in a to zero. 正如我的问题所暗示的那样,我的目标是找到b中所有小于1的值的索引,并将这些相同的a中的索引设置为零。
The first expression ie 第一个表达式即
a(find(b<1)) = 0
does what I want, but matlab is suggesting that I use logical indices to improve performance. 做我想要的,但是matlab建议我使用逻辑索引来提高性能。 Does the second expression do the same thing?
第二个表达式是否做同样的事情?
a(b(b<1)) = 0
No. 没有。
a(b<1) = 0
does the same thing. 做同样的事情。
b(b<1)
returns the values of b
where b
is smaller than 1. This is not a logical value (which it should be for logical indexing) and it is probably not of the same size as b
(unless all values are less than 1). 返回
b
小于1的b
的值。这不是逻辑值(对于逻辑索引应为逻辑值),并且可能与b
大小不同(除非所有值均小于1)。
find
returns the actual indices of elements values are less than a 1. on the other hand, b<1
returns vector with length equal to b and it has 0's for elements fulfill the condition and 1's for those does not fill fill condition. find
返回元素的实际索引值小于1。另一方面, b<1
返回长度等于b的向量,对于满足条件的元素,其值为0,对于未满足填充条件的元素,其值为1。 Let suppose, you have b vector: 假设您有b个向量:
b = [2 3 4 5 6 -1 9 -2]
find(b<2)
ans =
6 8
>> b<2
ans =
0 0 0 0 0 1 0 1
b(b<2)
ans =
-1 -2
a(b(b<1)) =0
Subscript indices must either be real positive integers or logicals.
So both operations are not same. 因此,两种操作都不相同。
b<1
returns the logical array and find(b)
returns the indices of elements fulfill condition. b<1
返回逻辑数组,而find(b)
返回满足条件的元素的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.