MATLAB Answers

Extract rows an put them in a different array based on specific column values.

1 view (last 30 days)
SRB
SRB on 4 Apr 2019
Commented: Dhanashree Mohite on 12 Apr 2019
I have a 1410 x 4 matrix. A truncated version is below:
399.010000000000 2 1 NaN
399.760000000000 16 NaN NaN
400.140000000000 16 NaN NaN
400.390000000000 16 NaN NaN
400.760000000000 16 NaN NaN
401.630000000000 NaN NaN 45.4277500000000
405.080000000000 NaN NaN 45.3810000000000
413.320000000000 NaN NaN 42.3262500000000
415.950000000000 NaN NaN 43.1500000000000
420.200000000000 NaN NaN 38.6815000000000
422.750000000000 NaN NaN 38.0767500000000
429.250000000000 NaN NaN 44.4442500000000
434.390000000000 1 NaN NaN
434.700000000000 NaN NaN 42.0570000000000
434.760000000000 1 NaN NaN
438.720000000000 NaN NaN 46.6870000000000
441.640000000000 16 NaN NaN
441.720000000000 NaN NaN 46.3582500000000
447.250000000000 NaN NaN 39.7590000000000
453.590000000000 NaN NaN 39.3142500000000
460.390000000000 NaN NaN 38.1947500000000
463.350000000000 NaN NaN 38.7207500000000
467.330000000000 64 NaN 37.8670000000000
...............................................................................
What I would like to do is extract all the rows that include a 16 in the second column as well as all the rows between 16 and a different number (pictured are instances where it would be between 16 and 1 or 16 and 64; however, it is possible for the other number to be 1, 2, 4, 8, 20, 21, 32, 64, or 128).
Below is what I would want from the above example:
399.760000000000 16 NaN NaN
400.140000000000 16 NaN NaN
400.390000000000 16 NaN NaN
400.760000000000 16 NaN NaN
401.630000000000 NaN NaN 45.4277500000000
405.080000000000 NaN NaN 45.3810000000000
413.320000000000 NaN NaN 42.3262500000000
415.950000000000 NaN NaN 43.1500000000000
420.200000000000 NaN NaN 38.6815000000000
422.750000000000 NaN NaN 38.0767500000000
429.250000000000 NaN NaN 44.4442500000000
441.640000000000 16 NaN NaN
441.720000000000 NaN NaN 46.3582500000000
447.250000000000 NaN NaN 39.7590000000000
453.590000000000 NaN NaN 39.3142500000000
460.390000000000 NaN NaN 38.1947500000000
463.350000000000 NaN NaN 38.7207500000000

  0 Comments

Sign in to comment.

Answers (1)

Bob Nbob
Bob Nbob on 10 Apr 2019
I'm a little confused what exactly you're looking for here, but hopefully I can at least get you started.
To retain all rows which contain 16 in the second column:
A = % Your data here
B = A(A(:,2) == 16,:);
If you want to adjust for other possible conditions you can add or statements (|), or and (&) statements, in order to account for those other conditions. For example, to include 'NaN' values as well:
B = A(A(:,2)==16 | A(:,2) == NaN,:);

  1 Comment

Dhanashree Mohite
Dhanashree Mohite on 12 Apr 2019
For comaprison with NaN, isnan will be helpful.
B = A(A(:,2)==16 | isnan(A(:,2)),:);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by