Incorrect dimensions for Matrix multiplication

25 views (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

Image Analyst
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?
  2 Comments
Izk
Izk on 21 Apr 2022
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 on 21 Apr 2022
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!