WHY LOGICAL INDEXING REDUCE TOTAL ROW COUNT
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2019 年 3 月 8 日
"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?
採用された回答
Steven Lord
2019 年 3 月 8 日
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)
その他の回答 (0 件)
参考
カテゴリ
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!