How to extract all rows that only one columns has a value greater than?

조회 수: 64 (최근 30일)
Hi all,
I have a quick question. I have A as a 5000 x 30 table. I want to to see which cells have a value greater than 50 and extract the entire corresponding row. How should I do that?
Thanks in advance.

채택된 답변

Star Strider
Star Strider 2021년 5월 21일
편집: Star Strider 2021년 5월 21일
One approach —
T1 = array2table(randi(60,15, 7))
T1 = 15×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 ____ ____ ____ ____ ____ ____ ____ 12 9 56 23 12 3 8 37 49 4 59 14 16 41 46 48 13 6 11 53 3 43 33 16 44 6 7 48 57 4 40 40 51 54 52 5 3 46 2 4 57 30 22 43 32 1 26 52 32 56 14 38 55 46 4 9 43 2 20 53 6 32 21 35 4 27 9 22 6 7 28 9 37 35 24 49 37 47 30 14 53 49 23 9 29 16 57 28 27 15 10 15 32 56 49 55 39 38 47 17 7 29 18 8 1
idx = any(T1{:,:}>50,2); % Logical Row Index
T1_extracted = T1(idx,:)
T1_extracted = 11×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 ____ ____ ____ ____ ____ ____ ____ 12 9 56 23 12 3 8 37 49 4 59 14 16 41 46 48 13 6 11 53 3 57 4 40 40 51 54 52 5 3 46 2 4 57 30 22 43 32 1 26 52 32 56 14 38 55 46 4 9 43 2 20 53 6 32 21 47 30 14 53 49 23 9 29 16 57 28 27 15 10 15 32 56 49 55 39 38
EDIT — (21 May 2021 at 15:52)
Added timetable operations and result —
T1T = [table(datetime('now')+hours(0:size(T1,1)-1)', 'VariableNames',{'Time'}) T1]
T1T = 15×8 table
Time Var1 Var2 Var3 Var4 Var5 Var6 Var7 ____________________ ____ ____ ____ ____ ____ ____ ____ 21-May-2021 15:50:17 12 9 56 23 12 3 8 21-May-2021 16:50:17 37 49 4 59 14 16 41 21-May-2021 17:50:17 46 48 13 6 11 53 3 21-May-2021 18:50:17 43 33 16 44 6 7 48 21-May-2021 19:50:17 57 4 40 40 51 54 52 21-May-2021 20:50:17 5 3 46 2 4 57 30 21-May-2021 21:50:17 22 43 32 1 26 52 32 21-May-2021 22:50:17 56 14 38 55 46 4 9 21-May-2021 23:50:17 43 2 20 53 6 32 21 22-May-2021 00:50:17 35 4 27 9 22 6 7 22-May-2021 01:50:17 28 9 37 35 24 49 37 22-May-2021 02:50:17 47 30 14 53 49 23 9 22-May-2021 03:50:17 29 16 57 28 27 15 10 22-May-2021 04:50:17 15 32 56 49 55 39 38 22-May-2021 05:50:17 47 17 7 29 18 8 1
TT1 = table2timetable(T1T);
idx = any(TT1{:,:}>50,2); % Logical Row Index
TT1_extracted = TT1(idx,:)
TT1_extracted = 11×7 timetable
Time Var1 Var2 Var3 Var4 Var5 Var6 Var7 ____________________ ____ ____ ____ ____ ____ ____ ____ 21-May-2021 15:50:17 12 9 56 23 12 3 8 21-May-2021 16:50:17 37 49 4 59 14 16 41 21-May-2021 17:50:17 46 48 13 6 11 53 3 21-May-2021 19:50:17 57 4 40 40 51 54 52 21-May-2021 20:50:17 5 3 46 2 4 57 30 21-May-2021 21:50:17 22 43 32 1 26 52 32 21-May-2021 22:50:17 56 14 38 55 46 4 9 21-May-2021 23:50:17 43 2 20 53 6 32 21 22-May-2021 02:50:17 47 30 14 53 49 23 9 22-May-2021 03:50:17 29 16 57 28 27 15 10 22-May-2021 04:50:17 15 32 56 49 55 39 38
.

추가 답변 (2개)

David Hill
David Hill 2021년 5월 21일
a=table2array(yourTable);
[idx,~]=find(a>50);
a=a(unique(idx),:);%this should give you what you are looking for
  댓글 수: 1
Wolfgang McCormack
Wolfgang McCormack 2021년 5월 21일
@David Hill thank you David. Any solution for timetables? because turning the time table to an array will cause a lot of mess for me. It's a sorted table based on time (with a lot of gaps) and turning it to an array won't allow me to use the histogram for monthyl values.

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


Image Analyst
Image Analyst 2021년 5월 21일
Try this:
% Convert your table tA to a double matrix called dA.
dA = table2array(tA);
% Find out which rows have any values more than 50.
rowsToTextract = any(dA > 50, 2)
% Extract only those rows. You can get a new double variable
% and/or a new table variable. I show both ways.
A50 = dA(rowsToTextract, :) % As a double matrix variable
t50 = tA(rowsToTextract, :) % As a table variable

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by