Subscripted assignment dimension mismatch

조회 수: 1 (최근 30일)
Catarina
Catarina 2014년 12월 17일
댓글: Guillaume 2014년 12월 17일
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...

채택된 답변

Thorsten
Thorsten 2014년 12월 17일
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
Guillaume
Guillaume 2014년 12월 17일
편집: Guillaume 2014년 12월 17일
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 2014년 12월 17일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by