Logical indexing when index exceeds matrix dimensions
5 views (last 30 days)
Show older comments
I am mentioning the following example as a case of logical indexing which I only came across very recently.
x = randi([1 2], 10, 1); % Create a random vector
x(2:4)= 100; % intentionally change few elements
idx = x>2; % index x>2
x(idx) = []; % delete x>2
x(idx) % NO ERROR!!
length(idx)==length(x)
It seems it is possible to index the vector x with the logical vector idx, which has a larger length than x. I would normally expect an error of the type: Index exceeds matrix dimensions. Apparently, no error is thrown from Matlab.
I don't understand the usefulness of this behavior. I have been using for quite some time logical indexing. However, only now I got to notice this strange behavior. I believe that this can lead into problems and bugs, since no error/warning is thrown. I would appreciate your comments. Thank you.
0 Comments
Answers (3)
Azzi Abdelmalek
on 17 Nov 2014
Edited: Azzi Abdelmalek
on 17 Nov 2014
Now try this example
x = randi([1 2], 10, 1)
x(5:10)= 100 % intentionally change few elements
idx = x>2 % index x>2
x(idx) = [] % delete x>2
x(idx) % NO ERROR!
In your example the size of x is 7x1. all element in idx with size 10x1 are zeros after the index 7
2 Comments
per isakson
on 17 Nov 2014
Edited: per isakson
on 18 Nov 2014
Yes, Matlab behaves as you describe
>> x = 1; % <1x1 double>
>> is = false( 1,100); % <1x100 logical>
>> is(1)=true;
>> x(is) % addressing x(1) works
ans =
1
>> is(2)=true; % addressing x(2) throws an error
>> x(is)
Index exceeds matrix dimensions.
The other way round; the logical index may be the shorter one
>> x = 1:12;
>> is = true;
>> x(true)
ans =
1
The documentation says: Using Logicals in Array Indexing,   [...] In this masking type of operation, every true element in the indexing array is treated as a positional index into the array being accessed. (My highlight. I haven't searched further.)
This piece of documentation doesn't say anything about positions, which have the value false.
The behavior honors the documentation, I think.
Whether or not it's useful that Matlab allows different sizes of array and logical index is another question.
RULE:   the positions of the true elements in the indexing array controls the result. Trailing false are ignored.
I've used logical indexing for a long time. Nevertheless, I have no opinion. In my code, I guess, it would be hard to find cases where the size of the logical index array,   is_my_choice,   differs from the size of the array,   A,   in a reference like
A( is_my_choice ) = 17;
"Isn't it prone to bugs?"   I cannot recall a case, in which a test for equal sizes would have uncovered a mistake in my code.
Giorgos Papakonstantinou
on 17 Nov 2014
1 Comment
per isakson
on 18 Nov 2014
Edited: per isakson
on 18 Nov 2014
"However, I am talking about difference in the sizes of the indexing and the matrix being accessed"   Communication is difficult. I tried hard to address this difference in size. It's large in my two examples.
Obviously, the rule is:   "every true element in the indexing array is treated as a positional index into the array being accessed." Trailing false are ignored. A logical indexing is legal as long as the position of the last true is within the length of the "array being accessed". Consequently the "indexing array" can have nearly any length.
Matlab has been commercially successful for thirty years. Matlab is a mixture of old and new. The Mathworks is reluctant to change anything that would affect many users. By this I try to say that "fundamental" things, which have been around for a long time, will hardly be changed. Whether strange or not.
I try to learn and remember how Matlab behaves and live with it.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!