While/for loop to write a set of values only when they are in a certain range.

2 views (last 30 days)
So, I have a variable called XY. An whos on that variable looks like this:
Name Size Bytes Class Attributes
XY 2897x2 46352 double
A snippet of that variable looks like is this:
A B
527 393
523 397
513 409
506 411
509 412
What I need is a while/for loop that will write the values of XY to a new variable (lets call it XY2) only when both A and B column is in certain value range. For instance, in the example above I only want to write A and B to excel, when both A and B are within 400-500. So I only want to write the last 3 sets, and not the 2 on the top. What is the easies way to go about this?

Accepted Answer

Jacob Halbrooks
Jacob Halbrooks on 12 Mar 2012
I would suggest a combination of ALL with logical indexing and masking:
dataToWrite = XY(all((XY >= 400) & (XY <= 500), 2),:);

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!