MATLAB: Preallocating a Matrix(For loop, Vectorization)

preallocation

I hear that preallocating a matrix before the for loop is efficeint , but why do i get the error " The value assigend to the variable might be unused", when used before vectorization ? Example code
%% Vectorization method
Y4=repmat(A,N,1);
Y4 = cell2mat(arrayfun(@(i) A^i, (1:N)', 'Uni', false)
%% Normal Multiplication method
Y5=repmat(A,N,1);
for i=1:N
Y5((i-1)*Dim+1:i*Dim,:)=A^i;
%disp i
end

Best Answer

In these lines:
Y4=repmat(A,N,1);
Y4 = cell2mat(arrayfun(@(i) A^i, (1:N)', 'Uni', false)
The first line assigns something to Y4. The second line wipes out what you just assigned to Y4 in the first line and replaces it with something else from the second line. In other words, the first line is useless and the value assigned to Y4 in the first line is unused.