MATLAB - Data is lost when closing the FOR loop

2

I'm having trouble with a loopback that counts the white pixels of a piece of an image and stores the position x and y of the piece and the total of white pixels. When I print the values inside the loop it works, but immediately after the loop the three arrays are zeroed. Can anyone help me?

Code:

y = zeros(altura*largura);
x = zeros(altura*largura);
v = zeros(altura*largura);


for j=0:altura-1
    for k=0:largura-1

        pedaco = f8(j*40+1 : j*40+40, k*40+1 : k*40+40); %binary piece
        pedac = im2uint8(pedaco);
        totalBrancos = sum(sum(pedac)); %sum white pixels

        pos = altura*j+k+1;

        y(pos) = j;
        x(pos) = k;
        v(pos) = totalBrancos;

        disp(y(pos)); %works
        disp(x(pos)); %works
        disp(v(pos)); %works

    end
end  

disp(y); %all zeros
disp(x); %all zeros
disp(v); %all zeros
    
asked by anonymous 27.06.2017 / 18:10

2 answers

0

I do not think they're all zeros.

On the first three lines, try the following:

y = zeros(altura*largura,1);
x = zeros(altura*largura,1);
v = zeros(altura*largura,1);

And then test the program again

    
04.07.2017 / 19:19
0

The answer given by Ataide --alias, I do not think all the elements will be zero either - it works in case you want to put all the points in a row, if the intention is to leave all points in an array, the code changes in some points:

In the statement:

y = zeros(altura,largura);
x = zeros(altura,largura);
v = zeros(altura,largura);

Inside the loop:

    y(j+1,k+1) = j;
    x(j+1,k+1) = k;
    v(j+1,k+1) = totalBrancos;

    disp(y(j+1,k+1)); %works

Note that in this case, the y and x parts end up with the value of their respective index subtracting 1.

    
11.09.2017 / 21:46