Matrix dimension must agree
1 次查看(过去 30 天)
显示 更早的评论
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
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
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!