Matrix dimension must agree

조회 수: 2 (최근 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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by