フィルターのクリア

What controls of this error 'Inner matrix dimensions must agree'?

2 ビュー (過去 30 日間)
Sandi J
Sandi J 2018 年 9 月 13 日
コメント済み: Dennis 2018 年 9 月 13 日
Sometimes I use these steps but without error
N=5;
s=linspace(0,2);
C=exp(s*(0:5));
Why sometimes other times this error appears ? Inner matrix dimensions must agree.
  2 件のコメント
Dennis
Dennis 2018 年 9 月 13 日
s is a vector of length 100, you want to multiply this vector with a vector of length 6.
What result do you expect?
Matt J
Matt J 2018 年 9 月 13 日
You probably set 's' to a scalar value in some situations. Only then will the code run without error.

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

採用された回答

Stephan
Stephan 2018 年 9 月 13 日
編集済み: Stephan 2018 年 9 月 13 日
When using * in Matlab with non-scalars, matlab follows the calculation rules of linear algebra:
>> A = randi(10,3,2)
A =
4 4
2 6
8 2
>> B = randi(10,2,5)
B =
7 7 8 1 10
3 7 5 3 2
>> C = A * B
C =
40 56 52 16 48
32 56 46 20 32
62 70 74 14 84
>> whos A B C
Name Size Bytes Class Attributes
A 3x2 48 double
B 2x5 80 double
C 3x5 120 double
Multiply a 3x2 Matrix with a 2x5 is possible and results in a 3x5 Matrix. If you try the same "backwards" it will give the error:
>> D = B * A
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 perform elementwise multiplication, use '.*'.
Read more in the documentation for Matrix operations, where this behavior is described in detail.
Best regards
Stephan
  3 件のコメント
Stephan
Stephan 2018 年 9 月 13 日

yes, but you asked for an explanation why sometimes you get the "inner dimensions error" and sometimes not - that was what i tried to answer.

Dennis
Dennis 2018 年 9 月 13 日

This will not return an error either:

N=5;
s=linspace(0,2);
C=exp(s'*(0:5));

The question is if this 'solves the problem'. It might be a way to calculate something that is not result that you want.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by