Subscripted assignment dimension mismatch

1 visualizzazione (ultimi 30 giorni)
Hello! I have the following problem: Matlab tells me there is a "subscripted assignment dimension mismatch". This the code I'm trying to run:
N = 1:20;
extrapolatedmaturities = 1:100;
Price = zeros(s,length(extrapolatedmaturities));
for t = 1:s
for i = 1:length(extrapolatedmaturities)
Price(t,:) = exp(-UFR*i)+...
(z(:,t)'*SWfunction(N,maturities2,alpha,UFR));
end
end
Where UFR is a number, z is a N by t matrix (which I invert) and SWfunction produces a N by N matrix. What I want matlab to do is to produce a t by i matrix where for all columns it computes exp(-UFR*i) and up to N add it to the second term, in which it should take for each t the row of values in z, multiply it by the NxN matrix from the SWfunction. Can someone help me? I've tried so many ways of doing this and it always tells me there is a dimension mismatch...

Risposta accettata

Thorsten
Thorsten il 17 Dic 2014
The term exp(-UFR*i) is indepent of t, so you could compute it as
for i = 1:length(extrapolatedmaturities)
expUFR(i) = exp(-UFR*i);
end
And then use
for t = 1:s
Price(t,:) = expUFR + z(:,t)'*SWfunction(N,maturities2,alpha,UFR);
end
  3 Commenti
Guillaume
Guillaume il 17 Dic 2014
Modificato: Guillaume il 17 Dic 2014
Actually, for even speedier calculation, you can calculate expUFR without a loop:
expUFR = exp(-UFR*(1:length(extrapolatedmaturaties)));
or even
expUFR = exp(-UFR*extrapolatedmaturaties); %since extraxxx is 1:100
Guillaume
Guillaume il 17 Dic 2014
And also, unless SWfunction output can change for the same input that can also be calculated once out of the loop, since it doesn't depend on t either:
expUFR = exp(-UFR*extrapolatedmaturaties)
SWvalue = SWfunction(N, maturities2, alpha, UFR);
for t = 1:s
Price(t, :) = expUFR + z(:, t)' * SWvalue;
end
And I'm sure even the t loop could be vectorised although I can't think how right now.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by