Unable to perform assignment because the size of the left side is 4-by-1 and the size of the right side is 4-by-4 Error.
1 view (last 30 days)
Show older comments
I know a lot of these types of questions have been asked.
I tried to do the whole preallocation thing, but I'm definately doing it wrong. Can anyone help me understand exaclty what preallocation is and how to fix it in this situation?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/274430/image.png)
1 Comment
Rik
on 1 Mar 2020
X-X0 is a 4x1 subtracted from a 4x4, so X0 is expanded to 4x4, which results in a 4x4 output of that line of code. You're trying to store that in a column of a matrix.
I have no clue what your intended outcome is (especially with the lack of comments), so I can't suggest an edit.
Answers (1)
Thiago Henrique Gomes Lobato
on 1 Mar 2020
The dimensions of your X point and your step are wrong (1x4 and 4x1), you can just transpose one of them to fix your problem
X = X-(j\f)' % Don't use inv to solve linear systems in matlab
Also, your residue should not be individualized but rather depend of the whole vector, so:
err = zeros(1,n);
...
err(i) = norm(X-Xo)
...
if err(i)<tolX
A preallocation is basically saying how much memory a specific variable will use. In some languages this is a must, in matlab most times an option, although you can drastically increase the efficiency of your code with it in cases where the variables are too big and you have many loops, because otherwise at every iteration matlab will have to generate a new array by itself at every iteration.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!