While/for loop to write a set of values, varying in each column, when they are both in a certain range.
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Jonathan Sullivan
2012-3-20
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!