How to get a scalar from MATLAB
55 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2024 年 11 月 27 日 12:41
移動済み: Stephen23
2024 年 11 月 27 日 12:57
"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:
採用された回答
Star Strider
2024 年 11 月 27 日 12:42
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 件のコメント
dpb
2024 年 11 月 28 日 16:04
編集済み: dpb
2024 年 11 月 28 日 20:01
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
その他の回答 (1 件)
dpb
2024 年 11 月 27 日 12:53
編集済み: dpb
2024 年 11 月 28 日 16:05
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!