Logical indexing in tables with multiple targets

25 次查看(过去 30 天)
Suppose you have a table such as the one below:
Sample = [0;1;1;2;2;2];
Data = [rand(1,5);rand(1,5);rand(1,5);rand(1,5);rand(1,5);rand(1,5)];
A = table(Sample,Data)
A = 6×2 table
Sample Data ______ ____________________________________________________________ 0 0.21426 0.64868 0.07936 0.58883 0.75634 1 0.36764 0.46345 0.56078 0.12871 0.19823 1 0.58486 0.18506 0.029363 0.31866 0.2336 2 0.53846 0.47363 0.99926 0.29129 0.61109 2 0.063376 0.99878 0.36276 0.1082 0.0017856 2 0.89091 0.85546 0.011542 0.98834 0.88992
Now say you want to extract only a subset a data that corresponds to certain Sample numbers. I know of a coup[le ways to do this.
You can do row-wise deletion if your table isn't too large and you know the row numbers of what you want to delete. Or you can just call the rows that you care about and assign it to a new table.
B = A;
B(1:3,:) =[]; %This deletes the Samples 0 and 1, but leaves 2.
B
B = 3×2 table
Sample Data ______ ____________________________________________________________ 2 0.53846 0.47363 0.99926 0.29129 0.61109 2 0.063376 0.99878 0.36276 0.1082 0.0017856 2 0.89091 0.85546 0.011542 0.98834 0.88992
B = A;
B = B(4:6,:)
B = 3×2 table
Sample Data ______ ____________________________________________________________ 2 0.53846 0.47363 0.99926 0.29129 0.61109 2 0.063376 0.99878 0.36276 0.1082 0.0017856 2 0.89091 0.85546 0.011542 0.98834 0.88992
But if your table is large, I prefer to use some logical indexing:
B = A;
B = A(A.Sample == 2,:)
B = 3×2 table
Sample Data ______ ____________________________________________________________ 2 0.53846 0.47363 0.99926 0.29129 0.61109 2 0.063376 0.99878 0.36276 0.1082 0.0017856 2 0.89091 0.85546 0.011542 0.98834 0.88992
Now what if I want to have multiple targets in the mask of my logical lindex. I know I can combine multiple logical indicies (indexes?) as below, but then how do I pass that along to the table?
B = A;
C = A.Sample == 0;
D = A.Sample == 2;
E = C|D
E = 6×1 logical array
1 0 0 1 1 1
B = B(B.Sample(E),:)
Error using ()
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Really, I am hoping there is a way to make the following workflow happen without going through for loops for each target of my logical index, but I might have to homebrew my own function to accomplish this.
C = [1,5,23]; % An array of the Sample numbers I'd like to keep in my new table
B = A(A.Sample == C,:);
If anyone knows a better way to do this so I can avoind clunky blocks in my script that will piecewise pull out the bits that I need, I would really appreciate it. Thanks in advance!

采纳的回答

Bruno Luong
Bruno Luong 2023-9-1
编辑:Bruno Luong 2023-9-1
Note that what I wrote here is very similar to standard array.
Sample = [0;1;1;2;2;2];
Data = [rand(1,5);rand(1,5);rand(1,5);rand(1,5);rand(1,5);rand(1,5)];
A = table(Sample,Data)
A = 6×2 table
Sample Data ______ ________________________________________________________ 0 0.51977 0.58779 0.9266 0.98092 0.28373 1 0.67344 0.67279 0.28825 0.015219 0.20328 1 0.051701 0.58111 0.43058 0.11597 0.78534 2 0.65281 0.99898 0.51989 0.75293 0.99619 2 0.44047 0.91429 0.78285 0.83373 0.61386 2 0.26829 0.64501 0.28681 0.32614 0.93582
B = A;
C = A.Sample == 0;
D = A.Sample == 2;
E = C|D
E = 6×1 logical array
1 0 0 1 1 1
B = B(E,:) % Fix your error
B = 4×2 table
Sample Data ______ ___________________________________________________ 0 0.51977 0.58779 0.9266 0.98092 0.28373 2 0.65281 0.99898 0.51989 0.75293 0.99619 2 0.44047 0.91429 0.78285 0.83373 0.61386 2 0.26829 0.64501 0.28681 0.32614 0.93582
C = [1,5,23]; % An array of the Sample numbers I'd like to keep in my new table
B = A(ismember(A.Sample,C),:) % This will meet your last wish
B = 2×2 table
Sample Data ______ ________________________________________________________ 1 0.67344 0.67279 0.28825 0.015219 0.20328 1 0.051701 0.58111 0.43058 0.11597 0.78534
  1 个评论
ASM
ASM 2023-9-1
Thank you! I can't believe I was blind to that small detail. I appreciate your fast response.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by