How to find x,y matrix using for sintax
Mostrar comentarios más antiguos
i have been working for my thesis about image segmentation. i have this matrix
a =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 0 0
0 0 0 0 1 1 1 1 1 0
0 0 0 0 0 1 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
and then i want to find the first x,y with binary 1 using for sintax this is the one i use but its not working
---------------------------------------------
function [x1,y1,x2,y2,x3,y3,x4,y4]=gedge(x)
[m,n]=size(x);
% upper edge xy
for i=1:m
for j=1:n
if(x(i,j)==1)
x1=i;
y1=j;
break
end
end
end
% left edge xy
for j=1:n
for i=1:m
if(x(i,j)==1)
x2=i;
y2=j;
break
end
end
end
% bottom edge xy
for i=m:-1:1
for j=n:-1:1
if(x(i,j)==1)
x3=i;
y3=j;
break
end
end
end
% right edge xy
for j=n:-1:1
for i=m:-1:1
if(x(i,j)==1)
x4=i;
y4=j;
break
end
end
end
%for the cropping
y=x(x1:x4,y2:y3);
-----------------------------------------------------------------
so i want to erase the 0 binary for my image segmentation. it will be this matrix:
a =
0 1 1 1 0
1 1 1 1 1
0 1 1 0 0
0 1 0 0 0
please help recorrect my sintax code. Thanks
Respuesta aceptada
Más respuestas (2)
Jonathan Sullivan
el 20 de Jun. de 2013
Editada: Jonathan Sullivan
el 20 de Jun. de 2013
It's really simple if you use the functions find and any. See below:
ind1 = find(any(x == 1,2),1);
ind2 = find(any(x == 1,2),1,'last');
ind3 = find(any(x == 1,1),1);
ind4 = find(any(x == 1,1),1,'last');
y = x(ind1:ind2,ind3:ind4);
1 comentario
rio
el 20 de Jun. de 2013
Andrei Bobrov
el 20 de Jun. de 2013
out = a(any(a,2),any(a));
1 comentario
rio
el 20 de Jun. de 2013
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!