How to get a scalar from MATLAB
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 27 Nov 2024
Déplacé(e) : Stephen23
le 27 Nov 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.'
"I want to know why this happen..."
You used TIMES, which is the wrong operation:
Réponse acceptée
Star Strider
le 27 Nov 2024
A=[1:50];
B=[1:50];
rp = dot(A,B)
It doesn’t care whether one may be a row vector and the other a column vector.
.
3 commentaires
dpb
le 28 Nov 2024
Modifié(e) : dpb
le 28 Nov 2024
A=[1:50]; B=A;
dot(A,B)
sum(A.*B)
A*B'
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
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
Plus de réponses (1)
dpb
le 27 Nov 2024
Modifié(e) : dpb
le 28 Nov 2024
A=[1:50];
B=[1:50];
rp1=A*B'
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...
0 commentaires
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!