18 views (last 30 days)

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
on 18 Apr 2020

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

.

Star Strider
on 20 Apr 2020

I am getting the expected result:

D = load('Temp_Rain table.mat');

Temp_Rain = D.Temp_Rain;

L = table2array(varfun(@(x)((x>=15) & (x<=50)), Temp_Rain(:,2)));

Temp_Rain_2 = Temp_Rain(L,:);

Then checking:

Before = Temp_Rain(1:10,:)

After = Temp_Rain_2(1:10,:)

produces:

Before =

10×2 table

VarName2 VarName3

________ ________

31.447 69.79

33.191 83.479

34.808 88.95

30.423 65.394

31.536 96.773

32.559 93.525

36.026 109.91

36.716 35.646

28.261 52.139

28.536 18.59

After =

10×2 table

VarName2 VarName3

________ ________

36.716 35.646

28.536 18.59

34.459 48.416

35.179 23.567

30.255 18.303

34.349 19.674

28.64 26.635

28.655 47.264

28.712 21.465

35.886 31.472

Other than what I already mentioned, I have no idea what could cause the problem you are seeing.

.

Sindar
on 18 Apr 2020

Edited: Sindar
on 18 Apr 2020

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)

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.