0

I am getting this error in MatLab when trying to fill in an array. MatLab is saying that the assignment B(j, 1) = z(z-counter) contains a negative or non integer value. However when I look at the variables values in the workspace after trying to execute the code.
j =1, counter =1, z =1

for j= 1:1:totalSizeOfMatrix
    if( j <= totalNumberInBottom)
        if( mod(j, lengthOfBottomRow) == 1)
            counter= counter +1;
        end
        z = mod(j,lengthOfBottomRow);
        B(j,1) = z(z-counter);
    end
    if( j > totalNumberInBottom && j <= totalNumberNotInTop)
        if( mod(j, lengthOfSecondRow) == 1)
            counter= counter +1;
        end
        z = mod(j,lengthOfSecondRow);
        B(j,1) = z(z-counter);
    end
    if( j > totalNumberInBottom)
        if( mod(j, lengthOfTopRow) == 1)
            counter= counter +1;
        end
        z = mod(j,lengthOfTopRow);
        B(j,1) = z(z-counter);
    end
end
shruti1810
  • 3,920
  • 2
  • 16
  • 28
  • 1
    MATALB index starts at 1. Therefore, z(0) is invalid. – Autonomous Feb 16 '14 at 21:19
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for a [generic approach](http://stackoverflow.com/a/20054048/983722) to deal with this error. – Dennis Jaheruddin Mar 10 '14 at 10:31

1 Answers1

1

If z = 1 and counter = 1, then z - counter is zero.

Therefore, z(z-counter) is trying to access z(0), which is an error.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720