Restricting matrix values
3 次查看(过去 30 天)
显示 更早的评论
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!
采纳的回答
Jan
2011-8-15
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), :);
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!