How does logical indexing work ?

326 次查看(过去 30 天)
Was going through the MATLAB Getting Started course when I came across logical indexing.
>> v = v1(v1 > 6)
stores in v the elements of v1 that are greater than 6. Alright, I get that. But why ?
v1(1) should refer to the 1st element of v1, right ? How does MATLAB differentiate between the two ? Or am I going wrong somewhere and not understanding something ?
  2 个评论
Bruno Luong
Bruno Luong 2018-10-3
MATLAB stores also the type of "number" (the correct term is CLASS); it the uses it to recognize when it logical (values from a boolean operation) or a real numbers (values from an arithmetic operation)
Adam
Adam 2018-10-3
Logical indexing is just a neat shortcut to avoid needing to use the 'find' function to create linear indices e.g.
v = v1( find( v1 > 6 ) )
would be equivalent and would be using linear indexing, but would be un-necessary as find is slow (relative to using logical indexing).

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2018-10-3
编辑:Stephen23 2018-10-3
v = v1(v1 > 6)
"stores in v the elements of v1 that are greater than 6."
Yes. Because that comparison returns a logical vector, and the position of the true values (shown as 1) in that logical vector determines which values to return.
"v1(1) should refer to the 1st element of v1, right ?"
Yes. Because 1 is a numeric (double, to be precise) and its value 1 it refers to the first element of v1.
"How does MATLAB differentiate between the two ?"
Simple: all logical indexing use the logical class, whereas all linear and subscript indexing use a numeric class:
That is all: the class of the input tells MATLAB what kind of indexing you are doing (for linear/subscript indexing the commas also make a difference). If you look at every single example of logical indexing, you will see that the index itself is always a logical vector (i.e. has class logical). That is how MATLAB knows.
  2 个评论
Bruno Luong
Bruno Luong 2018-10-3
编辑:Bruno Luong 2018-10-3
Example to illustrate the difference between logical and position indexing:
>> a=3:7
a =
3 4 5 6 7
>> b=a<10
b =
1×5 logical array
1 1 1 1 1
>> a(b)
ans =
3 4 5 6 7
>> i=double(b)
i =
1 1 1 1 1
>> a(i)
ans =
3 3 3 3 3
>> a(logical(i))
ans =
3 4 5 6 7
>>
Vikram Yaduvanshi
Vikram Yaduvanshi 2018-10-3
编辑:Vikram Yaduvanshi 2018-10-3
Perfect. Exactly what I was looking for, clears all my doubts. Thank you.

请先登录,再进行评论。

更多回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018-10-3
编辑:KALYAN ACHARJYA 2018-10-3
From the following example, you got the answer
v1=[1 2 3 4 5 6 7 8];
>> v=v1(v1>6)
v =
7 8
When linear indexing v1(1 up to the length of the v1)
Here v1 is variable name its v1(1) first element, v1(2) second element...elementwise
_ stores in v the elements of v1 that are greater than 6. Alright, I get that. But why ?_
here Matlab check the v1 in element wise,v only allows the v1 element values having greater than 6.
Always recomended to use Logical indexing for neat code and easily debug the code
Hope this help you!

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by