Removing rows of table based on logical array

조회 수: 32 (최근 30일)
Nicholas
Nicholas 2023년 7월 1일
편집: Peter Perkins 2023년 7월 17일
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!

채택된 답변

Voss
Voss 2023년 7월 1일
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
Nicholas
Nicholas 2023년 7월 4일
Thanks!
Peter Perkins
Peter Perkins 2023년 7월 17일
편집: Peter Perkins 2023년 7월 17일
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:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by