Most efficient way to find the indices of rectangular annuli in a 2D matrix

조회 수: 6 (최근 30일)
Hello,
I have a 2D matrix of dimensions 400 x 400. I am interested in extracting the indices of the regions marked in pink, green, and yellow in the below figure. One way to go about this would be: First, find the indices where the X > 100 & X < 300 & Y > 100 & Y < 300 and that is the pink region. Here, the Xs represent the columns and the Ys represent the rows. For the green and yellow regions, it will be useful if I can do something like: find the indices where the X > 50 & X < 350 & Y > 50 & Y < 350 and add an additional condition that the indices should not correspond to the pink region. Similarly, the yellow region is simply all the indices that do not correspond to the pink or green regions. How do I implement this?

채택된 답변

Matt J
Matt J 2020년 11월 5일
편집: Matt J 2020년 11월 5일
I=[ones(1,50), 2*ones(1,50), 3*ones(1,100)];
I=[I,flip(I)];
pink = (I==3 & I.'==3);
green = (I>=2) & (I.'>=2)&~pink;
yellow = ~(pink|green);
imshow(pink)
imshow(green)
  댓글 수: 9
Sai Prasanth
Sai Prasanth 2020년 11월 12일
Wouldn't this create a uniform grid of 381 x 381? And we started with a nonuniform grid of 414x 381. How will this work?
Matt J
Matt J 2020년 11월 12일
편집: Matt J 2020년 11월 12일
True. Well, then you could do
[~,Stripes]=ndgrid(1:414, ceil((1:381)/20)/20 ) ;
or you could do,
Stripes=repmat( ceil( (1:381)/20)/20) , 414,1)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by