Removing rows of table based on logical array
32 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Voss
on 1 Jul 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))
% 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
% remove those rows from the table:
data(TT,:) = []
2 Comments
Peter Perkins
on 17 Jul 2023
Edited: Peter Perkins
on 17 Jul 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:
More Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!