pre allocating variables in matlab

1 Ansicht (letzte 30 Tage)
emeka onwochei
emeka onwochei am 3 Mär. 2021
Kommentiert: emeka onwochei am 4 Mär. 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 Kommentare
James Tursa
James Tursa am 3 Mär. 2021
You need to give us a small example of input and desired output. Please describe specifically what it is about the output of your current code that you don't like. What are you trying to do?
emeka onwochei
emeka onwochei am 3 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 4 Mär. 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 Kommentar
emeka onwochei
emeka onwochei am 4 Mär. 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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by