Removing rows of table based on logical array

32 visualizaciones (últimos 30 días)
Nicholas
Nicholas el 1 de Jul. de 2023
Editada: Peter Perkins el 17 de Jul. de 2023
Hello, I have an imported table of data that is 122 x 11.
I have identified an xdata and ydata I want to use, and have named them such.
ydata comes from column 7 of my table.
I am singling out the heighest 10 values in this column by using
topTenData = maxk(ydata, 10)
that identified a minimum value of 1.01, and I'm uninterested in any data below that value.
I am attempting to get rid of all rows that are smaller than this using (remember column 7 is ydata):
TT = data(:,7) < 1.01
data(TT,:) = []
What I expect to happen is anywhere in which TT returns true, the row will be removed.
Instead, I get an error stating:" A table row subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string-array, a cell array of character vectors, or a pattern scalar."
Does this mean that if my raw data rows have a multitude of different types of data within each column, it is not possible to delete the rows this way? If so, can I get some guidance on a better method? Thanks!

Respuesta aceptada

Voss
Voss el 1 de Jul. de 2023
I think the problem is with this line:
TT = data(:,7) < 1.01
With data being a table, I get the error, "Undefined operator '<' for input arguments of type 'table'."
Instead of using parentheses ( ) you should use curly braces { } to access the contents of a table, as in:
TT = data{:,7} < 1.01
Then that line will run and TT will be a logical vector you can use to delete rows from the table as you intend.
Here's a simple complete example to illustrate:
% a table with two columns:
data = table(randi(10,10,1),randi(10,10,1))
data = 10×2 table
Var1 Var2 ____ ____ 8 7 9 6 5 4 1 2 6 10 1 6 7 3 4 10 3 6 10 2
% a logical vector saying whether the data in column 2 is less than 4.
% Note: use curly braces {} to access the contents of a table.
TT = data{:,2} < 4
TT = 10×1 logical array
0 0 0 1 0 0 1 0 0 1
% remove those rows from the table:
data(TT,:) = []
data = 7×2 table
Var1 Var2 ____ ____ 8 7 9 6 5 4 6 10 1 6 4 10 3 6
  2 comentarios
Nicholas
Nicholas el 4 de Jul. de 2023
Thanks!
Peter Perkins
Peter Perkins el 17 de Jul. de 2023
Editada: Peter Perkins el 17 de Jul. de 2023
Voss is correct up to a point, but there is some late-breaking R2023a news that changes the story. Please see my comment in another thread:

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by