Logical indexing when index exceeds matrix dimensions

5 views (last 30 days)
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.

Answers (3)

Azzi Abdelmalek
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
Giorgos Papakonstantinou
Giorgos Papakonstantinou on 17 Nov 2014
Thank you Azzi for your answer. I have observed that Matlab throws an error in this case. However, if all true values are padded at the beginning of the idx vector and the number of true values is greater or equal to the size of the smaller vector x then no error is thrown. In all other cases it throws an error. Simply because the:
'Index exceeds matrix dimensions'.
But also in the exception that I have posted, the same error message should be thrown, because the index has a larger dimension than the matrix.
Don't you agree that this is an ambiguous behavior? Isn't it prone to bugs?

Sign in to comment.


per isakson
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, &nbsp [...] 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: &nbsp 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, &nbsp is_my_choice, &nbsp differs from the size of the array, &nbsp A, &nbsp in a reference like
A( is_my_choice ) = 17;
"Isn't it prone to bugs?" &nbsp I cannot recall a case, in which a test for equal sizes would have uncovered a mistake in my code.
  1 Comment
Giorgos Papakonstantinou
Giorgos Papakonstantinou on 17 Nov 2014
Honestly, I wouldn't have discovered it, if it hadn't created a problem to my code.

Sign in to comment.


Giorgos Papakonstantinou
Giorgos Papakonstantinou on 17 Nov 2014
Thank you per. It is true what you say about the positions. However, I am talking about difference in the sizes of the indexing and the matrix being accessed. I don't know if the behavior is consistent with the definition, but the error thrown on every other case (apart from exceptions) is not consistent.
To my personal opinion it seems that it has been overlooked. Several things I have disliked in Matlab. However, none of them had a "strange" behavior. This is the first time that I come across with something so fundamental which doesn't seem to work "properly".
  1 Comment
per isakson
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" &nbsp Communication is difficult. I tried hard to address this difference in size. It's large in my two examples.
Obviously, the rule is: &nbsp "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.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!