Matrix dimension must agree

1 回表示 (過去 30 日間)
Maria Makri
Maria Makri 2019 年 10 月 13 日
回答済み: Prateekshya 2024 年 8 月 20 日
S=[1/2 8/10 9/10 1]
g=[1/18 1/4/12/9.5 1/29/12]
L=[S(1)-g(1) 0 1/(4*12) 0; g(1) S(2)-g(2) S(3)-g(3) 0; 0 0 g(3) S(4)]
t=1:4;
n=zeros(length(t),4);
n(1,:) = [100; 80; 100; 50];
t_final=200;
for i=1:t_final
n(i+1,:) = L.*n(i,:)';
end
Ι want to construct a Leslie matrix in matlab but when I multiply the n vector with the Matrix I keep getting this : Matrix dimensions must agree. I don't know how to fix it.
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 13 日
L is a 3 x 4 matrix. You are using .* against a 4 x 4 extracted down to 1 x 4 and then transposed to get a 4 x 1. What result are you expecting from using a 3 x 4 .* a 4 x 1 ?

サインインしてコメントする。

回答 (1 件)

Prateekshya
Prateekshya 2024 年 8 月 20 日
Hi Maria,
The error you are encountering is due to the incorrect use of element-wise multiplication (.*) instead of matrix multiplication (*). In MATLAB, when you want to multiply a matrix by a vector using matrix multiplication, you should use the "*" operator. Please refer to the below link for more information on Matrix Multiplication in MATLAB: https://www.mathworks.com/help/matlab/ref/mtimes.html
I hope this helps!

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by