Azzera filtri
Azzera filtri

While/for loop to write a set of values, varying in each column, when they are both in a certain range.

2 visualizzazioni (ultimi 30 giorni)
So, I asked a similar question earlier, but I had made an assumption about my data, that did not hold up. The variable I am working with 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. But this varies individually for column A and B. For instance, I ONLY want to write to XY2 values when A=510-530 and B=400-420. This would mean that I would only write the following value:
513 409
Las time I was given this code, which worked beautifully (in future works I think that I could use that one):
XY2 = XY(all((XY >= 165) & (XY <= 371), 2),:);
Credits given to user Jacob Halbrooks btw, for that code =)
My own attempts to alter this one, to make it fit with my data, did not work at all. My background is not in programming, so I am patching together my code with the help of forums like this, and stuff I have managed to figure out myself. I use the code to extract data for my master thesis.

Risposta accettata

Jonathan Sullivan
Jonathan Sullivan il 20 Mar 2012
You want to find the rows that satisfy your need.
inRangeX = XY(:,1) >= 510 & XY(:,1) <= 530; % First column between 510-530
inRangeY = XY(:,2) >= 400 & XY(:,2) <= 420; % Second column between 400-420
rowsOfInterest = inRangeX & inRangeY; % Both conditions must be satisfied
XY2 = XY(rowsOfInterest,:); % Store data in a new array.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by