Restricting matrix values

1 回表示 (過去 30 日間)
A
A 2011 年 8 月 15 日
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 件のコメント
Sean de Wolski
Sean de Wolski 2011 年 8 月 15 日
Entire column or entire row?
A
A 2011 年 8 月 15 日
Entire row, as in remove points(9,1) and points(9,2)

サインインしてコメントする。

採用された回答

Jan
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 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by