How to get a scalar from MATLAB

55 views (last 30 days)
Amy
Amy on 27 Nov 2024 at 12:35
Edited: dpb on 28 Nov 2024 at 20:01
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 Comments
Stephen23
Stephen23 on 27 Nov 2024 at 12:41
Moved: Stephen23 on 27 Nov 2024 at 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.'
ans = 42925
"I want to know why this happen..."
You used TIMES, which is the wrong operation:
Amy
Amy on 27 Nov 2024 at 12:51
Moved: Stephen23 on 27 Nov 2024 at 12:57
I've tried that but cannot work. It's strange, but thanks!

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 27 Nov 2024 at 12:42
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 Comments
Star Strider
Star Strider on 27 Nov 2024 at 12:59
As always, my pleasure!
dpb
dpb on 28 Nov 2024 at 16:04
Edited: dpb on 28 Nov 2024 at 20:01
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

Sign in to comment.

More Answers (1)

dpb
dpb on 27 Nov 2024 at 12:53
Edited: dpb on 28 Nov 2024 at 16:05
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...

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!