"Matrix dimensions must agree"

The command
B1 = [B zeros(size(B,1), 1)]
produces a matrix when entered in the command window, but when I try to run the same code in the editor I get an error message reading "matrix dimensions must agree." What do I have to change for the code to work here as well?

3 个评论

What size is B? And how does it arrive in your code in the editor. Is it the same as it was on the command line?
B is a 3 x 2 matrix and yes, the code is the exact same in the command line and in the editor.
I need to make B a 3 x 3 matrix.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2017-5-30
编辑:Jan 2017-5-30
The code might be the same in the command window and in the script or function, but the data are not. Either the script/function contains a variable called "zeros" or "size", or B is a local function. It is sure, that there is a difference, which you didn't mention, most likely because you are not aware of it. Without seeing the complete code and the complete error message, we cannot guess, what happens. So please post the relevant part of the code.
Or examine the problem by your own by using the debugger. Type this in the command window:
dbstop if error
Run your code again until it stops at the problem. Now check the sizes manually:
B
which size
which zeros
size(B)
size(B, 1)
zeros(2,3)
zeros(size(B,1), 1)
[B zeros(size(B,1), 1)]
What do you observe? Which part causes the problem?

类别

在 帮助中心 和 File Exchange 中查找有关 Desktop 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by