Incorrect dimensions for Matrix multiplication
25 views (last 30 days)
Show older comments
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 Comment
Steven Lord
on 22 Apr 2022
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.
Accepted Answer
Image Analyst
on 21 Apr 2022
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?
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!