Remove rows from table when number in cell array is above certain value

조회 수: 5 (최근 30일)
Sam Acker
Sam Acker 2019년 3월 25일
답변: Peter Perkins 2019년 4월 2일
I would like to remove rows of a table when the number inside the text field of one column is greater than some number: For example:
test_table = table();
test_table.Var1 = {'A-01'; 'A-01'; 'A-01'; 'A-02'; 'A-02'; 'A-02'; 'A-03'; 'A-03'; 'A-03';};
test_table.Var2 = [1; 2; 3; 1; 2; 3; 1; 2; 3];
threshold = 2;
I would like to remove all rows with 'A-03' since 03 > threshold (in this case, 2).
While I found that extractAfter was useful for pulling off the numbers in the Var1 column, I'm having trouble converting these to a number array that I can compare logically to the Var2 column.
Thanks in advance for all the help!

답변 (1개)

Peter Perkins
Peter Perkins 2019년 4월 2일
I suggest you try using a categorical variable:
>> test_table.Var1 = categorical(test_table.Var1,{'A-01' 'A-02' 'A-03'},'Ordinal',true)
test_table =
9×2 table
Var1 Var2
____ ____
A-01 1
A-01 2
A-01 3
A-02 1
A-02 2
A-02 3
A-03 1
A-03 2
A-03 3
>> threshold = 'A-02';
>> test_table(test_table.Var1 > threshold,:)
ans =
3×2 table
Var1 Var2
____ ____
A-03 1
A-03 2
A-03 3
>> test_table(test_table.Var1 >= threshold,:)
ans =
6×2 table
Var1 Var2
____ ____
A-02 1
A-02 2
A-02 3
A-03 1
A-03 2
A-03 3
You might then say that I've just givewn you the opposite problem of the one you had before, but I bet it's easy to get from 2 to 'A-02' using sprintf.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by