filling regions of matrix

3 vues (au cours des 30 derniers jours)
Mohammad Golam Kibria
Mohammad Golam Kibria le 26 Juin 2011
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

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 26 Juin 2011
Iout = bwlabel(~cumsum(I,2))+1
  2 commentaires
Oleg Komarov
Oleg Komarov le 27 Juin 2011
great! +1
Mohammad Golam Kibria
Mohammad Golam Kibria le 27 Juin 2011
Thanks

Connectez-vous pour commenter.

Plus de réponses (1)

the cyclist
the cyclist le 26 Juin 2011
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 commentaire
Mohammad Golam Kibria
Mohammad Golam Kibria le 27 Juin 2011
This also works fine.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by