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일
A = diag(1./C))*B :-)
or, only with matrices,
A = diag(C)\B
Hylke Dijkstra
Hylke Dijkstra 2022년 1월 28일
If I understand it correctly the second option, only with matrices, does not work with column vectors?
I get the error message matrix dimensions must agree.
The first option works just fine though, but includes element wise division which I want to avoid.
Torsten
Torsten 2022년 1월 28일
If B and C are column vectors of the same length,
A = diag(C)\B
should work fine.
Hylke Dijkstra
Hylke Dijkstra 2022년 1월 28일
I think the dimensions match, yet I do not get the ame results for wrhs and wrhs2.
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일

0 개 추천

Backslash \ , not divide /.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 1월 28일
편집: Image Analyst 2022년 1월 28일

0 개 추천

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

Perhaps I did not explain it properly
A=B./C
This works just fine!
I don't want to divide them if they do not match. That is the point here basically, but sometimes mistakes are easily made and if they do not match using generalized matrix multiplication or division using the point notation always generates results. Using matrix division gives me an error message if the dimensions do not match.
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
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!
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 1월 28일

댓글:

2022년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by