Elementwise division in matrix notation
显示 更早的评论
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 个评论
Hylke Dijkstra
2022-1-28
Torsten
2022-1-28
If B and C are column vectors of the same length,
A = diag(C)\B
should work fine.
Hylke Dijkstra
2022-1-28
Hylke Dijkstra
2022-1-28
采纳的回答
更多回答(1 个)
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
2022-1-28
Image Analyst
2022-1-28
Yes, sometimes mistakes are made, and wouldn't you want an error to be thrown in those situations? Or would you rather not know about them? Anyway, it can't decide how to proceed upon an error unless you tell it. Like
try
A = B ./ C;
catch ME
% Some code to handle different length vectors, whatever that might be.
end
Usually it doesn't just try to "fix" the problem automatically, which is a good thing because its fix may not be what you were hoping it would do.
Hylke Dijkstra
2022-1-28
Not using diag() will also give an error. Look:
B = rand(10, 1);
C = rand(2, 1);
A = B ./ C;
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
