How to get a scalar from MATLAB

조회 수: 20 (최근 30일)
Amy
Amy 2024년 11월 27일
편집: dpb 2024년 11월 28일
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
Stephen23 2024년 11월 27일
이동: Stephen23 2024년 11월 27일
"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 2024년 11월 27일
이동: Stephen23 2024년 11월 27일
I've tried that but cannot work. It's strange, but thanks!

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

채택된 답변

Star Strider
Star Strider 2024년 11월 27일
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
Star Strider
Star Strider 2024년 11월 27일
As always, my pleasure!
dpb
dpb 2024년 11월 28일
편집: dpb 2024년 11월 28일
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

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

추가 답변 (1개)

dpb
dpb 2024년 11월 27일
편집: dpb 2024년 11월 28일
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...

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by