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 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2020-3-1
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.
回答(1 个)
Thiago Henrique Gomes Lobato
2020-3-1
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!