Incorrect dimensions for Matrix multiplication

25 次查看(过去 30 天)
This is my code: I am getting an error in line 31.
The following is the error message, image has also been attatched:
Error using . Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in cw_code_alpha [line 31].
A=diag(ap*ones(nx,1),1)+diag(aw*ones(nx-1,1),-1)+diag(ae*ones(nx-1,1),1)
Code below:
clc
clear
close all
%% initialisation
alpha1=0.02;
a=0.6;
b=0.05;
c=0.01;
q=0.05;
g=20;
L=4;
tmax=100;
dx=0.01;
nx=L/dx+1;
dt=0.005;
nt=tmax/dt+1;
x=0:dx:L;
t=0:dt:tmax;
A=zeros(nx,nt);
T=zeros(nx,1);
b=zeros(nx,1);
v=zeros(nt,1);
S=zeros(nx,1);
A=diag(ap*ones(nx,1),1)+diag(aw*ones(nx-1,1),-1)+diag(ae*ones(nx-1,1),1);
  1 个评论
Steven Lord
Steven Lord 2022-4-22
The code you posted should throw an error because the variables ap, aw, and ae are not defined. If you show what those variables contain (or at least what size they are and what size they should be, which may be different) we may be able to offer better suggestions about what you could do to avoid these errors.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2022-4-21
So did you try what it said "To perform elementwise multiplication, use '.*'."
A=diag(ap .* ones(nx,1),1)+diag(aw .* ones(nx-1,1),-1)+diag(ae .* ones(nx-1,1),1)
If not, why didn't you try it? Are you 100% sure you want a matrix multiplication instead of an element-by-element multiplication?
  2 个评论
Izk
Izk 2022-4-21
Yes, could the problem be in the initialisation? Could you confirm it is ok, I will include a PNG file of the question.
Izk
Izk 2022-4-21
I tried it your way, and still get the same error. Also I don't think I can use element x element because Aw and Ae are a range of values.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by