How does one use a string vector to select rows from a table. The vector length and the table height are unequal

1 vue (au cours des 30 derniers jours)
The elements of vector (X) correspond to the 1st column of the table (T).
lenght(X) << height(T)
I want to use X to extract the corresponding rows the table
Note that the vector elements are unique e.g. X = ["AGAP004746", "AGAP004753", "AGAP004756"]
Some of the rows are shown below. The elements of T(:, 1) are not unique.
AGAP004746 CPIJ018891-PA 1:1 1082603
AGAP004747 CPIJ011183-PA 1:1 1002379
AGAP004749 CPIJ006245-PA 1:1 1102192
AGAP004750 CPIJ002918-PA 1:1 689782
AGAP004752 CPIJ009694-PA 1:1 1177233
AGAP004753 CPIJ001266-PA 1:m 788759
AGAP004753 CPIJ011603-PA 1:m NA
AGAP004754 CPIJ002923-PA 1:1 1170952
AGAP004755 CPIJ002926-PA 1:1 NA
AGAP004756 CPIJ002925-PA 1:m 1082201
AGAP004756 CPIJ011812-PA 1:m NA
The expected output should be Table T1 below
AGAP004746 CPIJ018891-PA 1:1 1082603
AGAP004753 CPIJ001266-PA 1:m 788759
AGAP004753 CPIJ011603-PA 1:m NA
AGAP004756 CPIJ002925-PA 1:m 1082201
AGAP004756 CPIJ011812-PA 1:m NA

Réponse acceptée

Voss
Voss le 14 Août 2023
X = ["AGAP004746", "AGAP004753", "AGAP004756"];
idx = ismember(T{:,1},X);
T1 = T(idx,:);

Plus de réponses (1)

Daniel Bengtson
Daniel Bengtson le 14 Août 2023
Modifié(e) : Daniel Bengtson le 15 Août 2023
T = %your table
X = {"AGAP004746", "AGAP004753", "AGAP004756"};
T1 = T(cell2mat(cellfun(@(x) any(strcmp(x,X)),T{:,1},'UniformOutput',false)),:);
  2 commentaires
George
George le 15 Août 2023
T is 11915x4 table
"AGAP004677" "CPIJ004710-PA" "1:1" "1185504"
"AGAP004679" "CPIJ008043-PA" "1:1" ""
"AGAP004682" "CPIJ006146-PA" "1:1" ""
"AGAP004685" "CPIJ000737-PA" "1:1" "1053699"
"AGAP004686" "CPIJ004712-PA" "1:1" "682105"
etc.
X is a 106x1 string
"AGAP000088"
"AGAP000192"
"AGAP000193"
"AGAP000194"
"AGAP000284"
"AGAP000818"
etc
Assuming that you mean T and X instead of t and x in line 3 of your script,
T1 = T(cell2mat(cellfun(@(X) any(strcmp(X,VectorEls)),T{:,1},'UniformOutput',false)),:)
T1 = 0×4 empty table
Daniel Bengtson
Daniel Bengtson le 15 Août 2023
Good catch on the t -> T mistake, but x is just a function handle and would have been fine. I stole that line from something else that I was working on and was sloppy with variables. Edited for correction.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by