Trying to extract rows of a matrix where a given column equals a range of values

1 view (last 30 days)
Hi there, I am trying to extract rows of a matrix where the 2nd column of that matrix equals a range of values (i.e., 36-48 for example). I am then only interested in column 9 of those rows.
For example, here is some of the code I have been trying:
YFP_WithmCherry_0_plus = enlistcell_new_YFP_WithmCherry(enlistcell_new_YFP_WithmCherry(:,2)==36:48,9);
I keep getting this error: "The logical indices in position 1 contain a true value outside of the array bounds."
The following code does work, however it doesnt extract all of the rows i want:
YFP_WithmCherry_0_plus = enlistcell_new_YFP_WithmCherry(enlistcell_new_YFP_WithmCherry(:,2)==36,9);
Ive attached the data frame. Please help! Thank you.

Accepted Answer

Voss
Voss on 10 Mar 2024
Edited: Voss on 10 Mar 2024
X = load('enlistcell_new_YFP_WithmCherry.mat').enlistcell_new_YFP_WithmCherry
X = 262×9
1.0e+05 * 0.0000 0.0003 0.0000 0.1906 0.0005 0.0106 0.0006 0.0687 0.0009 0.0001 0.0003 0.0001 1.2818 0.0006 0.0464 0.0014 0.2298 0.0010 0.0001 0.0003 0.0001 0.1430 0.0004 0.0234 0.0009 0.0616 0.0010 0.0002 0.0003 0.0000 0.7342 0.0005 0.0304 0.0009 0.3569 0.0010 0.0002 0.0003 0.0000 0.1044 0.0002 0.0251 0.0008 0.0485 0.0010 0.0002 0.0003 0.0000 0.1170 0.0002 0.0253 0.0008 0.0757 0.0010 0.0003 0.0003 0.0000 0.1763 0.0005 0.0143 0.0005 0.1006 0.0010 0.0001 0.0003 0.0000 0.5223 0.0005 0.0288 0.0009 0.1402 0.0009 0.0001 0.0003 0.0000 0.1957 0.0003 0.0254 0.0008 0.1131 0.0010 0.0001 0.0003 0.0000 0.3763 0.0004 0.0304 0.0009 0.1727 0.0010
"2nd column ... equals a range of values (i.e., 36-48... ) ... column 9 of those rows"
One way:
idx = X(:,2) >= 36 & X(:,2) <= 48;
result = X(idx,9)
result = 5×1
99.0246 98.7840 92.8824 98.0198 97.4417
Another way, since all elements in that range in column 2 are integers:
idx = ismember(X(:,2),36:48);
result = X(idx,9)
result = 5×1
99.0246 98.7840 92.8824 98.0198 97.4417

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!