Selecting particular data range from table/ columns

조회 수: 30 (최근 30일)
Tyson Campbell
Tyson Campbell 2020년 4월 18일
댓글: Star Strider 2020년 4월 20일
Hi there,
I am very new to Matlab and haven't been able to find the following solution.
I have two columns within a table of 1537090 x 2. Within the second column I want to extract numbers that fall within a range of 15 to 50.
Any numbers that are found in that range in column 2 I would like the corresponding row from Column 1 to also be pulled out.
The result will be another two column table but have all values within 15 to 50 from Column 2 and the corresponding rows from Column 1.
Thanks very much in advance and really appreciate any help.

채택된 답변

Star Strider
Star Strider 2020년 4월 18일
Try this:
T1 = table(rand(100,1), randi(75,100,1)); % Original Table
L = table2array(varfun(@(x)((x>=15) & (x<=50)), T1(:,2))); % Logical Vector
T2 = T1(L,:); % Edited Table
.
  댓글 수: 10
Tyson Campbell
Tyson Campbell 2020년 4월 20일
I reran the code using what you just sent through then and it worked! Temp_Rain_2 resulted in a table of 419751x2. Thanks so much for all your efforts.
Star Strider
Star Strider 2020년 4월 20일
As always, my pleasure!
(That was the same result I got.)

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

추가 답변 (1개)

Sindar
Sindar 2020년 4월 18일
편집: Sindar 2020년 4월 18일
idx = (mytable{:,2} >= 15) & (mytable{:,2} <= 50);
newtable = mytable(idx,1:2);
Components of the answer:
  • extract the data in the 2nd column (note {}; () would return a table)
mytable{:,2}
  • identify which elements of this array (i.e. which rows of the table) are >= 15. This returns a logical array (true where >=15, false elsewhere)
(mytable{:,2} >= 15)
  • elementwise AND with the other condition (<=50) so true only in the range
() & ()
  • extract the appropriate rows from the table (and columns, though ":" would work as well if you want all the columns)
mytable(idx,1:2)

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by