Error with matrix calculation

% matrix calculation
M3 = [0 0 0 0 -1 0 0 0 0 0 1 0;
0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 -0.05 1;
1 0 0 0 0 0 0 0 -1 0 -1 0;
0 -1 0 0 0 0 0 0 0 -1 0 0;
-0.15 0 0 0 0 0 0 0 -0.15 0 0.05 0;
-1 0 1 0 0 0 0 0 0 0 0 0;
0 1 0 1 0 0 0 0 0 0 0 0;
-0.4*sind(20) -0.4*cosd(20) -0.4*sind(20) 0.4*cosd(20) 0 0 0 0 0 0 0 0;
0 0 0 -1 0 0 0 1 0 0 0 0;
0 0 (BD/2)*cosd(Angle_FBD) (BD/2)*sind(Angle_FBD) 0 0 (BD/2)*cosd(Angle_FBD) (BD/2)*sind(Angle_FBD) 0 0 0 0];
N3 = [mass_body1*Acceleration_G1x;
mass_body1*Acceleration_G1y+mass_body1*9.81;
mass_body1*radius_body1^2*alpha_1;
mass_body2*Acceleration_G2x;
mass_body2*Acceleration_G2y+mass_body2*9.81;
(1/12)*mass_body2*radius_body2^2*alpha_2;
mass_body3*Acceleration_G3x;
mass_body3*Acceleration_G3y+mass_body3*9.81;
(1/12)*mass_body3*radius_body3^2*alpha_3;
mass_body4*Acceleration_G4x;
mass_body4*Acceleration_G4y+mass_body4*9.81;
(1/12)*mass_body4*BD^2*alpha_4];
X = M3/N3
It says that there is an error using "/" and that matrix dimensions must agree. I dont know exactly what this means, and can't find where the issue is. The error is in the line "X = M3/N3"

4 件のコメント

Steven Lord
Steven Lord 2024 年 5 月 2 日
What are the sizes of M3 and N3 immediately prior to executing that line of code? Insert these commands on the lines before the one where the error is thrown and show us what they display.
size(M3)
size(N3)
Tobias
Tobias 2024 年 5 月 3 日
A =
11 12
B =
12 1
Tobias
Tobias 2024 年 5 月 3 日
M3 is A and N3 is B
Tobias
Tobias 2024 年 5 月 3 日
I forgot a row with values in M3, but it still doesnt work. M3 is now 12 12, and N3 is 12 1

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

回答 (2 件)

James Tursa
James Tursa 2024 年 5 月 2 日

1 投票

We don't have all the variables necessary to run your code, but guessing that maybe you need element-wise divide instead of matrix divide?
X = M3 ./ N3
Note the period, so try ./ instead of just /

1 件のコメント

Tobias
Tobias 2024 年 5 月 3 日
thanks, it worked!

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

Walter Roberson
Walter Roberson 2024 年 5 月 3 日

0 投票

When you use the / (matrix right divide) operator, the two operands must have the same number of columns. The resulting matrix will have a size that is the number of rows of the first operand, by the number of rows of the second operand.
A = rand(5,3); B = rand(7,3);
A / B
ans = 5x7
0 0 0 0 1.8427 1.2240 -0.1906 0 0 0 0 -0.3170 0.8279 0.7349 0 0 0 0 1.1372 0.7934 0.5501 0 0 0 0 0.2419 1.0085 -0.2503 0 0 0 0 0.2136 1.2041 -0.0009
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You appear to be constructing an 11 x 12 matrix and dividing it by a 12 x 1 matrix. None of the operators \ .\ / or ./ would be suitable for that purpose.
It would not be an error to calculate
X = M3/N3'
which would then be an 11 x 12 matrix divided by a 1 x 12 matrix, which would give an 11 x 1 result.

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2023b

タグ

質問済み:

2024 年 5 月 2 日

コメント済み:

2024 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by