Logical indexing when index exceeds matrix dimensions
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
Azzi Abdelmalek
2014-11-17
编辑:Azzi Abdelmalek
2014-11-17
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 个评论
per isakson
2014-11-17
编辑:per isakson
2014-11-18
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
2014-11-17
1 个评论
per isakson
2014-11-18
编辑:per isakson
2014-11-18
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!