MATLAB Answers

Restricting matrix values

8 views (last 30 days)
A
A on 15 Aug 2011
I have a matrix:
points=
37 29
36 30
37 30
35 31
36 31
37 31
36 32
37 32
46 58
I need to restrict the values of the points such that
26<points(:,1)<46
21<points(:,2)<41
I want to remove the entire column if the above condition is not held.
E.g: (46,58) at position 9 is outside the boundaries, so I want points(9,:)=[]
I have written a very roundabout way of doing this:
find1=find(points(:,1)<(26))
if ~isempty(find1)
for i=1:length(find1)
points(find1(i),:)=[];
end
end
find2=find(points(:,1)>(46))
if ~isempty(find2)
for i=1:length(find2)
points(find2(i),:)=[];
end
end
find3=find(points(:,2)<(21))
if ~isempty(find3)
for i=1:length(find3)
points(find3(i),:)=[];
end
end
find4=find(points(:,2)>(41))
if ~isempty(find4)
for i=1:length(find4)
points(find4(i),:)=[];
end
end
However this method breaks if for a position "pos" points(pos,1) and points(pos,2) both exceed boundaries.
Does anyone have a cleaner way of doing this?
Thank you in advance for your responses!
  2 Comments
A
A on 15 Aug 2011
Entire row, as in remove points(9,1) and points(9,2)

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Aug 2011
points = [37 29; ...
36 30; ...
37 30; ...
35 31; ...
36 31; ...
37 31; ...
36 32; ...
37 32; ...
46 58];
keep1 = and(26 < points(:,1), points(:,1) < 46);
keep2 = and(21 < points(:,2), points(:,2) < 41);
points = points(and(keep1, keep2), :);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by