필터 지우기
필터 지우기

Incorrect Dimensions for matrix multiplication

조회 수: 95 (최근 30일)
Aijalon Marsh
Aijalon Marsh 2023년 10월 13일
편집: Sam Chak 2023년 10월 13일
Ld=(0:0.001:5);
L=0.007;
Ta=25;
h=375;
Kf=175;
Kd=0.032;
Tb=75;
Ap=3.02*10^-3;
N=238;
Ac=10^-6;
m=92.58;
Lf=L-Ld;
Rtf=(cosh(m*Lf)+(h/(m*Kf))*sinh(m*Lf))/(sqrt(4*h*(1)^3)*(sinh(m*Lf)+h/(m*Kf))*cosh(m*Lf));
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 operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
disp(Rtf)
why do i keep getting an error on the second to last line?

답변 (3개)

James Tursa
James Tursa 2023년 10월 13일
편집: James Tursa 2023년 10월 13일
Because you are using matrix operators instead of element-wise operators. Try this:
Rtf=(cosh(m.*Lf)+(h./(m.*Kf)).*sinh(m.*Lf))./(sqrt(4*h.*(1)^3).*(sinh(m.*Lf)+h./(m.*Kf)).*cosh(m.*Lf));
Notice the dots. * and / are matrix operations, and .* and ./ are element-wise operations.
Not sure why you have (1)^3

Sam Chak
Sam Chak 2023년 10월 13일
편집: Sam Chak 2023년 10월 13일
Ld=(0:0.001:5); % <-- array is first defined here, but does not appear in Rtf
L=0.007;
Ta=25;
h=375;
Kf=175;
Kd=0.032;
Tb=75;
Ap=3.02*10^-3;
N=238;
Ac=10^-6;
m=92.58;
Lf=L-Ld; % <-- Lf array is derived from Ld array, and it appears in Rtf
% To perform element-wise division & multiplication of arrays, the dots (.) are in indicated below
Rtf=(cosh(m*Lf)+(h/(m*Kf))*sinh(m*Lf))./(sqrt(4*h*(1)^3)*(sinh(m*Lf)+h/(m*Kf)).*cosh(m*Lf));
% ^ ^
% disp(Rtf)
plot(Ld, Rtf), grid on, xlabel('Ld'), ylabel('Rtf')

Torsten
Torsten 2023년 10월 13일
편집: Torsten 2023년 10월 13일
You multiply and divide arrays elementwise when you compute Rtf. Thus you have to use elementwise multiplication (.*) and elementwise division (./) :
Rtf=(cosh(m*Lf)+(h/(m*Kf))*sinh(m*Lf))./(sqrt(4*h*(1)^3)*(sinh(m*Lf)+h/(m*Kf)).*cosh(m*Lf));
instead of
Rtf=(cosh(m*Lf)+(h/(m*Kf))*sinh(m*Lf))/(sqrt(4*h*(1)^3)*(sinh(m*Lf)+h/(m*Kf))*cosh(m*Lf));
Take a look here for more details:

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by