How to multiply each value of a matrix by a number
이전 댓글 표시
Hello, Right now I am getting a matrix at the end but all the values in each row are the same. Im not sure why this is happening. My results matrix is right but the unhedged and hedged are doing this error.
k_g = [0.66, 0.65, 0.64, 0.63, 0.62, 0.61, 0.60, 0.59, 0.55];
c_g = [0.085855 0.032191 0.020795 0.017001 0.013711 0.010851 0.008388 0.006291 0.001401];
k_b = [1.30, 1.25, 1.20, 1.15, 1.10, 1.05, 1.00, 0.95, 0.90];
c_b = [0.137213 0.082645 0.045060 0.028338 0.016146 0.007860 0.003277 0.001134 0.000245];
results_G = zeros(length(k_g), length(c_g));
results_B = zeros(length(k_b), length(c_b));
unhedged_G = zeros(length(k_g), length(c_g));
unhedged_B = zeros(length(k_b), length(c_b));
Eg = 643000000;
Eb = 272000000;
corr = 0.675;
n = 100000;
Rg = normrnd(0,9,[n,1]);
Rb = normrnd(0,11,[n,1]);
for j = 1:length(c_g)
for i = 1:length(k_g)
G = 0.6531*(1+Rg(i)/100);
netpayoff_g = (k_g(i)-G)-c_g(j);
results_G(i,j)= netpayoff_g;
unhedged_G(i,j)=Eg.*G;
end
j=j+1;
end
for b = 1:length(c_b)
for a = 1:length(k_b)
B = 1.234*(1+Rb(a)/100);
netpayoff_b = (k_b(a)-B)-c_b(b);
results_B(a,b)= netpayoff_b;
unhedged_B(a,b)= Eb.*B;
end
b=b+1;
end
hedged_G = unhedged_G + (9.*results_G)
hedged_B = unhedged_B + (9.*results_B)
hedged = hedged_G + hedged_B
results_G
results_B
best_G = max(results_G(:))
best_B = max(results_B(:))
댓글 수: 3
jessupj
2020년 9월 16일
i think new versions of matlab don't complain about dimension mismatch when you add a row vector and a column vector, returning a matrix
e.g.
>> [1 1 1] + [1 2 3]'
ans =
2 2 2
3 3 3
4 4 4
try something like
unhedged_G(:) + (9*results_G(:))
to force both addends into row vectors
Dylan Springer
2020년 9월 16일
jessupj
2020년 9월 16일
it might simply LOOK like the numbers are the same because Eg and Eb are large and the answer is displayed in scientific notation. try looking at the results after you run
format long g
답변 (1개)
Sai Sri Pathuri
2020년 9월 22일
You may try the following:
Create Rg and Rb as matrices with same length as results matrices
Rg = normrnd(0,9,[length(k_g), length(c_g)]);
Rb = normrnd(0,11,[length(k_b), length(c_b)]);
For the calculation of unhedged_G, define G as
G = 0.6531*(1+Rg(i,j)/100);
For the calculation of unhedged_B, define B as
B = 1.234*(1+Rb(a,b)/100);
카테고리
도움말 센터 및 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!