dimanche 30 avril 2017

Matlab executes the first statement even if is false

I have the following function in matlab. I am trying to shuffle a matrix. But somehow matlab keeps executing the code from if even if the statement should go to else. And when it comes to else I have to put some aditional code, even if all it does is **i+1. It is normal for matlab or am I missing something?

function magic_matrix = magicsquare1(matrix,n)
magic_matrix=ones(n,n)*(-1);
i=3-1;
j=4+1;

for ki=1:n
for kj=1:n
    if(i<1)
        i=n;
    end
    if(j>n)
        j=1;
    end
    if magic_matrix(i,j) == -1

        magic_matrix(i,j)=matrix(ki,kj);
        %X = sprintf('i=%d j=%d',i,j);
        %disp(X)
        i=i-1;
        j=j+1;
    else
        i=i+2;
        j=j-1;
        if(i>n)
            i=1;
        end
        if(j<1)
            j=n;
        end
        magic_matrix(i,j)=matrix(ki,kj);
       % X = sprintf('i=%d / j=%d',i,j);
        %disp(X)
        i=i-1;
        j=j+1;
    end
end
end

Aucun commentaire:

Enregistrer un commentaire