0

Matlab show this error: "The variable 'result' appears to change size on every loop iteration. Consider preallocation for speed"

Where is my mistake?

Code:

function result = gaussSolutionOfSLAY( Matrix, Vector )

sizeMatrix = size(Matrix);
rows = sizeMatrix(1);
columns = sizeMatrix(2);

extended = [Matrix Vector];

for k = 1:rows                
    if (extended(k,k) ~= 0)
        extended(k,:) = extended(k,:)./extended(k,k);   
        for i = k + 1:columns           
            extended(i,:) = extended(i,:) - extended(k,:)*extended(i,k);
        end
    else
        disp('Division by thero');
        return
    end
end

result = extended(rows, columns);   
for k = rows - 1:-1:1
    result = [extended(k, columns + 1) - sum(extended(k, columns - (length(result) - 1):c) * result) result];    
end

end
A. Donda
  • 8,381
  • 2
  • 20
  • 49
AlexO
  • 11
  • 3
  • @user3037421, please do not re-use an old post for a new question. If you have a new question, make a new post. This post used to be about an mlint warning wrt preallocation, now it is about an error message. Don't do this! – Moreover, please write questions and their titles such that they indicate the actual problem. The title "Gauss to solve the set of equations" does not indicate what you are asking. – A. Donda Feb 23 '14 at 15:07
  • Since the user made a new post for his new question, I rolled back his edit of this one, restoring the original question. – A. Donda Feb 23 '14 at 15:54

1 Answers1

0

If there is no other error in your code, this usually comes as a warning. It happens when you defined an array with a size and you use extra lines or columns of this array. If you do this, your code runs slowly. This is a kind of new thing in Matlab (maybe the last 3 or three versions). It happens when you change "result".

DanielTheRocketMan
  • 3,199
  • 5
  • 36
  • 65