using the table format, how to filter on words

4 visualizaciones (últimos 30 días)
Erik Sund
Erik Sund el 12 de Feb. de 2015
Respondida: ag el 13 de Nov. de 2024 a las 18:24
My table looks like this:
Place1 Place2 Place3 ...
A A C
B A A
C C B
C C A
A C A
I want to remove all rows not containing say 'B' and ending up with
Place1 Place2 Place3
B A A
C C B
strcmp(table.Place1,{'B'}) % works in one column
strcmp(table(:,{'Place1','Place2','Place3'}),{'B'}) % returns 0

Respuestas (1)

ag
ag el 13 de Nov. de 2024 a las 18:24
Hi Erik,
To filter the rows containing 'B', you can use logical indexing as described in the below steps:
  1. Use "ismember" function to check each element of the array to see if it matches 'B'.
  2. Use "any" function to check if there is at least one true value in each row of the logical array produced by ismember. This operation should be performed across the second dimension (i.e., columns), meaning it checks each row for any occurrence of true.
The below code snippet illustrates the above approach:
axis = 2 % coloumns
rowsWithB = any(ismember(table{:,:}, 'B'), axis);
For more details, please refer to the following MathWorks documentation:
Hope this helps!

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by