"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 Comments

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.

Sign in to comment.

Answers (1)

Jan
Jan on 30 May 2017
Edited: Jan on 30 May 2017
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?

Categories

Asked:

on 30 May 2017

Edited:

Jan
on 30 May 2017

Community Treasure Hunt

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

Start Hunting!