Include rows containing specific value

11 visualizaciones (últimos 30 días)
Ninad Varkhede
Ninad Varkhede el 29 de Ag. de 2024
Respondida: Lei Hou el 9 de Sept. de 2024
I have a 1400*9 table (1400 rows and 9 columns) in MATLAB workspace. Each column has a different name. I want to select rows containing specific value. For example, how can I create a new table containig all rows with value of 0.456 in the column named "Biology"? I tried to use following code, but it gave error.
newData=[Data(find(Data.Biology == 0.456, :))]

Respuesta aceptada

Star Strider
Star Strider el 30 de Ag. de 2024
the value you are searching for. 0.456, may not be the exact value.
Use ismembertol instead of find to search for values within the chosen tolerannce.
Example —
Data = array2table(randn(10,9), 'VariableNames',{'A','Biology','C','D','F','G','H','I','J'});
Data{[2 5 9],2} = 0.456 + randn(3,1)*1E-9 % Create Inexact Values
Data = 10x9 table
A Biology C D F G H I J ________ ________ _________ ________ ________ _________ _________ _________ ________ -0.56073 0.66848 -1.6558 0.73911 -0.95053 -0.52805 0.73014 0.15877 -1.7607 1.8967 0.456 -0.074398 -0.19429 -0.13925 -1.0212 -0.78319 -1.2845 -0.21244 0.457 0.32445 -0.32174 -0.43284 -0.68997 0.23693 -0.046639 0.092096 1.0111 -0.96723 -2.0929 2.8776 -2.4453 1.2181 4.221 1.6101 -1.4944 0.01197 -0.1844 0.456 2.1279 -0.77057 -0.18804 1.9395 0.39835 -0.019314 -0.27927 -0.5154 -2.3019 -1.4235 -1.064 0.82205 -0.75171 -0.43085 -0.74567 -1.2876 -0.52872 1.059 0.83066 -0.56786 1.5213 0.0041647 -0.29605 0.89714 0.083718 -0.99425 -0.46588 0.33117 -0.71454 0.64947 0.33089 0.33116 -0.49755 1.2063 -0.40857 0.456 0.45269 0.35374 -0.11113 0.66758 -0.40103 -0.051921 0.27027 -0.85864 0.85976 0.74998 1.487 -0.1691 1.3689 -1.0675 -0.26557 0.83313
idx = ismembertol(Data.Biology, 0.456, 1E-4)
idx = 10x1 logical array
0 1 0 0 1 0 0 0 1 0
numidx = find(idx)
numidx = 3x1
2 5 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
NewData = Data(numidx,:)
NewData = 3x9 table
A Biology C D F G H I J ________ _______ _________ ________ ________ _______ ________ _________ ________ 1.8967 0.456 -0.074398 -0.19429 -0.13925 -1.0212 -0.78319 -1.2845 -0.21244 -0.1844 0.456 2.1279 -0.77057 -0.18804 1.9395 0.39835 -0.019314 -0.27927 -0.40857 0.456 0.45269 0.35374 -0.11113 0.66758 -0.40103 -0.051921 0.27027
The ismembertol function returns a logical vector. To get numeric indices, use find with it.
You may need to adjust the tolerance to work with your data, however this approach should work.
.
  2 comentarios
Ninad Varkhede
Ninad Varkhede el 30 de Ag. de 2024
Thank you so much. Your solution worked.
Star Strider
Star Strider el 30 de Ag. de 2024
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Lei Hou
Lei Hou el 9 de Sept. de 2024
rowfilter is used to select rows of a table meeting a specific condition. Here is the code might be useful in your case.
>> rf = rowfilter(Data);
>> Data(rf.Biology==0.456,:)
Hoping this is helpful to you.

Categorías

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

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by