필터 지우기
필터 지우기

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

조회 수: 2 (최근 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.

채택된 답변

Jonathan Sullivan
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by