Matrix dimensions must agree when doing an elementwise operation

a = 0:1:100;
b = 0:1:100;
c = 0:1:360;
r = 3.3*10^-2;
d = (a+b).*cos(c).*r./2;
Hello, I would like to compute the program with the following code above
When I run the program, I get this error like this.
could anyone explain me what's wrong?

回答 (2 件)

John D'Errico
John D'Errico 2022 年 4 月 11 日
編集済み: John D'Errico 2022 年 4 月 11 日

1 投票

How many elements are there in the vector a? (101)
How many elements are there in the vector b? (101)
How many elements are there in the vector c? (361)
Addition of a+b works, because there are the same number of elements in the two vectors.
But then elementwise multiplication would have MATLAB multiply EVERY element of (a+b) with the corresponding element of cos( c). How will that be expected to work? (Poorly.)
MATLAB has no problem mutiplying by r/2, because r is a scalar.
If you want to fix that, then a,b,c all need to have the same number of elements.

1 件のコメント

Michael Andersen
Michael Andersen 2022 年 4 月 11 日
thank you for the answer,
actually i want to find "d" value from a,b, and c data every row like this picture
do you know how to solve? thanks

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

Torsten
Torsten 2022 年 4 月 11 日
編集済み: Torsten 2022 年 4 月 11 日

0 投票

Yes, a,b and c must have the same number of elements for that an elementwise multiplication makes sense.
Maybe you mean
d = (a+b).'*cosd(c)*r/2;
?

2 件のコメント

Michael Andersen
Michael Andersen 2022 年 4 月 11 日
thank you for the answer,
actually i want to find "d" value from a,b, and c data every row like this picture
do you know how to solve? thanks
Torsten
Torsten 2022 年 4 月 11 日
a = 0:1:100;
b = 0:1:100;
c = 0:1:360;
r = 3.3*10^-2;
[A,B,C] = ndgrid(a,b,c);
D = (A+B).*cosd(C)*r/2

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

カテゴリ

タグ

質問済み:

2022 年 4 月 11 日

コメント済み:

2022 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by