filling regions of matrix

조회 수: 2 (최근 30일)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 6월 26일
Hi have a matrix.
I =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Here ones in the matrix has divided the matrix in 3 regions.Is it possible to have the following type of output:
I =
2 2 2 1
2 2 1 1
2 1 1 1
1 1 1 1
3 1 1 1
3 3 1 1
3 3 3 1
Thanks

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 6월 26일
Iout = bwlabel(~cumsum(I,2))+1
  댓글 수: 2
Oleg Komarov
Oleg Komarov 2011년 6월 27일
great! +1
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 6월 27일
Thanks

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

추가 답변 (1개)

the cyclist
the cyclist 2011년 6월 26일
There might be more efficient ways, but this will get the job done. I hope I didn't just do your homework for you.
I = [0 0 0 1; ...
0 0 1 0; ...
0 1 0 0; ...
1 0 0 0; ...
0 1 0 0; ...
0 0 1 0; ...
0 0 0 1];
[M,N] = size(I);
J = I;
for nj = 1:N
% Find the ones in this column. Algorithm OK even if there's only one of them.
firstOne = find(I(:,nj),1,'first');
lastOne = find(I(:,nj),1,'last');
J(1:firstOne-1, nj) = 2;
J(firstOne:lastOne,nj) = 1;
J(lastOne+1:end, nj) = 3;
end
J
  댓글 수: 1
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 6월 27일
This also works fine.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by