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
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
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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
