MATLAB Answers

pre allocating variables in matlab

조회 수: 27(최근 30일)
emeka onwochei
emeka onwochei 3 Mar 2021
댓글: emeka onwochei 4 Mar 2021
i have this block of code but when i access elements, they are not arranged properly
x=zeros(1,10000);
y=zeros(1,10000);
count=1;
for i=1:rows
for j=1:columns
if imgThresh(i,j)==0
y(count)=rows-i+1;
x(count)=j;
count=count+1;
break
end
end
end
nonzeros(x)
nonzeros(y)
  댓글 수: 2
emeka onwochei
emeka onwochei 3 Mar 2021
it supposed to read the binary image pixels and stores the cell of the first black box, it uses the points to draw a shape. it gives me the blue curve below.

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

답변(1개)

Walter Roberson
Walter Roberson 4 Mar 2021
That's not what I get.
imgThresh = im2bw(imread('image.png'));
rows = size(imgThresh,1);
columns = size(imgThresh,2);
x=zeros(1,10000);
y=zeros(1,10000);
count=1;
for i=1:rows
for j=1:columns
if imgThresh(i,j)==0
y(count)=rows-i+1;
x(count)=j;
count=count+1;
break
end
end
end
plot(x,y)
  댓글 수: 1
emeka onwochei
emeka onwochei 4 Mar 2021
this is for the second part,
for i=1:rows
for j=fliplr(1:columns)
if imgThresh(i,j)==0
y(count)=rows-i+1;
x(count)=j;
count=count+1;
break
end
end
end

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

Community Treasure Hunt

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

Start Hunting!

Translated by