Elementwise division in matrix notation

25 ビュー (過去 30 日間)
Hylke Dijkstra
Hylke Dijkstra 2022 年 1 月 28 日
コメント済み: Image Analyst 2022 年 1 月 28 日
Dear all,
I am trying to write the following code in matrix notation:
A = B. / C;
A, B and C are column vectors with 1435 rows.
I have already managed to do this with multiplication, i.e.:
D = E.*F;
This is equivalent to
D = diag(F)*E;
Also in this case D, E and F are also column vectors with 1435 rows.
I want to do this because it is more foolproof and does not give results if the dimensions do not match.
Best,
Hylke
  6 件のコメント
Torsten
Torsten 2022 年 1 月 28 日
編集済み: Torsten 2022 年 1 月 28 日
Maybe of interest if you want to get an error message when using incompatible elementwise operations:
de.mathworks.com/help/coder/ug/optimizie-implicit-expansion-in-generated-code.html
I corrected the internet address.
Hylke Dijkstra
Hylke Dijkstra 2022 年 1 月 28 日
This page does not seem to exist.. Thanks though, it would be helpful.

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

採用された回答

Torsten
Torsten 2022 年 1 月 28 日
Backslash \ , not divide /.
  1 件のコメント
Hylke Dijkstra
Hylke Dijkstra 2022 年 1 月 28 日
Oh dear, you're a savior.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 1 月 28 日
編集済み: Image Analyst 2022 年 1 月 28 日
Try getting rid of the space between the dot and the slash:
A = B ./ C;
If the length of B and C don't match, you might want to consider if you even want to divide them. Like, WHY don't they match? If one is shorter to you want to just assign the "left over" elements to something specific, like 0 or 1 or B or C or something?
  4 件のコメント
Hylke Dijkstra
Hylke Dijkstra 2022 年 1 月 28 日
I do want an error if I accidentally elementwise multiplicate or divide a matrix when the dimensions do not match. That is why I prefer to use matrix tricks (such as using a diagonal matrix) rather than use generalized element wise multiplication or division (i.e. using a dot).
Using matrix tricks gives an error message if I messed up, which I do want!
Image Analyst
Image Analyst 2022 年 1 月 28 日
Not using diag() will also give an error. Look:
B = rand(10, 1);
C = rand(2, 1);
A = B ./ C;
Arrays have incompatible sizes for this operation.

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

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by