I’m writing a simple MATLAB program to solve a project Euler problem.
The program creates a 900 x 900 matrix. Before creation of this matrix c by the program, I pre-allocate it in the following way:
c = zeros(900,900);
This yields an orange error message: “The value assigned to variable ‘c’ might be unused”.
Later in the program the matrix c is filled with numbers. So why the error message?
This is an mlint WARNING message. Not truly an error. An error will prevent your code from running. The mlint warnings merely suggest an inefficiency, a point where your code was possibly not efficiently written.
There is no need to preallocate an array that will then be reallocated. In fact, your first assignment is useless here. Later on in your code you then defined c as the result of a product of two vectors. As such, matlab completely ignores what you did in the first step. So that statement was indeed wasted and therefore should be dropped.
In general, only preallocate an array where you will later on assign only individual elements (or small groups of elements) of that array, perhaps in a loop.