Incorrect dimensions for Matrix multiplication

25 Ansichten (letzte 30 Tage)
Izk
Izk am 21 Apr. 2022
Kommentiert: Steven Lord am 22 Apr. 2022
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 Kommentar
Steven Lord
Steven Lord am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 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 Kommentare
Izk
Izk am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by