WHY LOGICAL INDEXING REDUCE TOTAL ROW COUNT

2 views (last 30 days)
Dear MATLAB,
I notice strange thing happen when use logical indexing. Specifically, the number of row is reduced.
Is this a bug? I am using MATLAB2017a
Thanks in advance for any insight.
The code are as follow
load('help_ismember'); % attached together with this question
NonNanPVT_Logic=ismember(Validtn_Loc,Validtn_Loc_MIA); % 37 row
KSS_valid_WithNan(NonNanPVT_Logic==0)=ev.KSS_valid(NonNanPVT_Logic==0); %Output reduced to 36
  2 Comments
Stephen23
Stephen23 on 8 Mar 2019
"Is this a bug?"
Not really, This behavior is easy to demonstrate:
>> X = [true,true,false,false];
>> A(X) = 3
A =
3 3
What size would you expect A to be?
polo sepian
polo sepian on 14 Mar 2019
Yes, you are right, it is more about how I position the variable.
Really appreciate for your time.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 8 Mar 2019
I suspect KSS_valid_WithNan didn't exist before that last line and that the last element of NonNanPVT_Logic == 0 was false.
x = (1:10).'
y = mod(x, 2) == 1 % Select odd numbers
z(y) = x(y)
The elements in the (non-existent) vector z to which values were assigned were 1, 3, 5, 7, and 9. Even though y has 10 elements, the last true value was in position 9.
If you want to control the size (and shape) of the vector z, preallocate it before assigning into it. For instance, if you want it to be exactly the same size and shape as x call zeros, ones, etc. passing the size(x) as the size of the array to create.
x = (1:10).'
y = mod(x, 2) == 1 % Select odd numbers
z2 = zeros(size(x));
z2(y) = x(y)

More Answers (0)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!