필터 지우기
필터 지우기

I need to delete some rows in an array based on if their column values fall inside a range

조회 수: 2 (최근 30일)
I have a Nx3 matrix A. I have my conditions: xlo = 0; xhi = 100; ylo = 0; yhi = 100; zlo = 0; zhi = 100; I want to be able to go through the matrix and start at row 1, and check that the value in A(1:1) falls between xlo and xhi if it does not I want it to delete that row and move to the next row. In case that the value in A(1:1) falls between xlo and xhi I want it to move to A(1:2) and I want the value in this cell to fall between ylo and yhi, if it doesnt I want it to delete the row and move to the next one. In case the value in A(1:2) falls within the ylo and yhi range then I want it to move to check A(1:3). A(1:3) should be in the range between zlo and zhi. If it is not in between the range then delete the row and move to the next row. If all conditions are satisfactory I want it to output that row into a new matrix that is going to be a Mx3. M being the number of all the rows that passed the conditions

채택된 답변

Stephan
Stephan 2018년 10월 24일
편집: Stephan 2018년 10월 24일
Hi,
to get Matrix B (B contains every row of A that meet all conditions) use:
B = A(A(:,1)>xlo & A(:,1)<xhi & A(:,2)>ylo & A(:,2)<yhi & A(:,3)>zlo & A(:,3)<zhi,:)
see this example with random numbers:
xlo = 0;
xhi = 100;
ylo = 0;
yhi = 100;
zlo = 0;
zhi = 100;
A = randi(200,25,3);
B = A(A(:,1)>xlo & A(:,1)<xhi & A(:,2)>ylo & A(:,2)<yhi & A(:,3)>zlo & A(:,3)<zhi,:);
results in:
A =
105 140 43
25 41 77
36 134 6
142 89 95
167 87 67
7 36 196
152 39 112
192 124 170
69 54 82
128 112 93
69 189 166
44 143 199
158 136 105
145 192 186
56 156 148
117 122 114
85 190 194
19 12 165
5 54 192
99 198 130
56 155 76
68 96 96
58 137 183
35 84 3
80 77 32
B =
25 41 77
69 54 82
68 96 96
35 84 3
80 77 32
Deleting rows in A which not meet the conditions gives the same result by using or instead of and:
A(A(:,1)<=xlo | A(:,1)>=xhi | A(:,2)<=ylo | A(:,2)>=yhi | A(:,3)<=zlo | A(:,3)>=zhi,:) = []
Result keeps the same - so removing lines in A is not needed to achieve what you want:
A =
25 41 77
69 54 82
68 96 96
35 84 3
80 77 32
Best regards
Stephan

추가 답변 (1개)

Jose Martinez
Jose Martinez 2018년 10월 24일
%TEST%
A = CROP;
B = A(A(:,1)>xlo & A(:,1)<xhi & A(:,2)>ylo & A(:,2)<yhi & A(:,3)>zlo & A(:,3)<zhi,:);
%TEST END%
That is how I used your code and it is outputting a matrix B with no values.
This is what I was thinking off earlier but its giving me an error:
nr = 0;
nf = 0;
for nr = 0:f-1
nr = nr + 1;
if (CROP(nr:1) >= xlo) && (CROP(nr:1) <= xhi)
if (CROP(nr:2) >= ylo) && (CROP(nr:2) <= yhi)
if (CROP(nr:3) >= zlo) && (CROP(nr:3) <= zhi)
nf = nf +1;
CFinal(nf,:) = CROP(nr,:);
else CROP(nr,:) = [];
end
else CROP(nr,:) = [];
end
else CROP(nr,:) = [];
end
end
but by some reason I am getting this error in the command window:
Operands to the || and && operators must be convertible to logical scalar values.
Error in MartensiteSim (line 117)
if (CROP(nr:2) >= ylo) && (CROP(nr:2) <= yhi)
  댓글 수: 3
Stephan
Stephan 2018년 10월 24일
Please accept helpful answers in order to help people with similar Problems find helpful answers.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Hypothesis Tests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by