How to get a scalar from MATLAB

20 visualizaciones (últimos 30 días)
Amy
Amy el 27 de Nov. de 2024
Editada: dpb el 28 de Nov. de 2024
If I have two matrices w is A 1*50 matrix and B is a 1*50 matrix. I want to use this formula of rp= w'r to calculate the result. This is the original formula to calculate the portfolio return. In this formula, w is an n*1 matrix and r is also an n*1 matrix. So, w' is a 1*n matrix. Thus we can get a scalar. But if I want to replace w' with A and replace r with B and I want to get a scalar in MATLAB. But I wrote codes below, it turn into a 50*50 matrix.
I want to know why this happen and how can I switch the matrix into a scalar.
A=[1:50];
B=[1:50];
rp1=A .* B.';
And if I use rp=A.*B. It will becomes a 1*50 matrix. Can I sum this matrix to get the scalar? (But it cannot make sence write?)
Thanks in advance!
A=[1:50];
B=[1:50];
rp2=A .* B.;
sum(rp2);
  2 comentarios
Stephen23
Stephen23 el 27 de Nov. de 2024
Movida: Stephen23 el 27 de Nov. de 2024
"But it cannot make sence write?"
Matrix multiplication does make sense, therefore you can use MTIMES:
A = 1:50; % removed the superfluous square brackets
B = 1:50; % removed the superfluous square brackets
A * B.'
ans = 42925
"I want to know why this happen..."
You used TIMES, which is the wrong operation:
Amy
Amy el 27 de Nov. de 2024
Movida: Stephen23 el 27 de Nov. de 2024
I've tried that but cannot work. It's strange, but thanks!

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 27 de Nov. de 2024
Probably the easiest way to do what you want is to use the dot function —
A=[1:50];
B=[1:50];
rp = dot(A,B)
rp = 42925
It doesn’t care whether one may be a row vector and the other a column vector.
.
  3 comentarios
Star Strider
Star Strider el 27 de Nov. de 2024
As always, my pleasure!
dpb
dpb el 28 de Nov. de 2024
Editada: dpb el 28 de Nov. de 2024
A=[1:50]; B=A;
dot(A,B)
ans = 42925
sum(A.*B)
ans = 42925
A*B'
ans = 42925
All ":work" if both A, B are row vectors for a slightly different interpretation of what the expressions mean.
As noted in earlier Answer, In order for the mathematics of the original paper to be correct, rp= w'r, then both r and w have to be column vectors.To follow the paper's convention you would instead have written
A=[1:50].'; B=A; % create A, B to mimic w, r from paper
A.'*B
ans = 42925
Alternatively, one can use MATLAB syntax convention even if for some other reason were to stick with row vectors by use of the colon expansion rule--
A=[1:50]; B=A; % both row vectors
A(:)'*B(:) % return as columns
ans = 42925

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 27 de Nov. de 2024
Editada: dpb el 28 de Nov. de 2024
A=[1:50];
B=[1:50];
rp1=A*B'
rp1 = 42925
Matrix multiplication is defined such that the resultant matrix is the size of the outer dimensions while the inner dimensions must be conforming , thus
[1xN].' * [1xN] --> [Nx1] * [1xN] --> [NxN]
whereas
[1xN] * [1xN].' --> [1xN] * [Nx1] --> [1x1]
In MATLAB the "dot" operator, .* is element-wise multiplication of conforming arrays or matrices.
In the original, to produce a single element, the w vector would have to have been a column vector, not a row vector, in which case the [Nx1]' operation would create the needed [1xN].
ERRATUM: Actually, both w and r must be column vectors for the original math to be correct...amplified in a comment above...

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by