How to find x,y matrix using for sintax
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (2)
Jonathan Sullivan
am 20 Jun. 2013
Bearbeitet: Jonathan Sullivan
am 20 Jun. 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 Kommentar
rio
am 20 Jun. 2013
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!