フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Number of elements must be the same?

2 ビュー (過去 30 日間)
Phil Whitfield
Phil Whitfield 2018 年 5 月 16 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
function Sf = FreeBoundary(S,t,V,K)
Sf = zeros(1,length(t));
eps_star = K*1e-5;
for j = 1:length(t)
Sf(j) = S(find(abs(V(:,j)-K+S)< eps_star, 1, 'last'));
end
end
I am trying to calculate an american put free boundary before plotting it on my graph. However when I use :
FreeBoundary(1:120,1/3,0.25,60)
I keep getting the error
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in FreeBoundary (line 7) Sf(j) = S(find(abs(V(:,j)-K+S)< eps_star, 1, 'last'));
Any chance you know how to fix it? Thanks

回答 (2 件)

Steven Lord
Steven Lord 2018 年 5 月 16 日
Are you certain that at each iteration of your for loop at least one element in abs(V(:,j)-K+S) is strictly less than eps_star?
Did you intend to subtract K and add S to V(:, j) or did you mean to subtract the quantity (K+S) from V(:, j)? In the latter case, I would write abs(V(:, j)-(K+S)).

Jan
Jan 2018 年 5 月 16 日
Sf = NaN(1,length(t));
for j = 1:length(t)
index = find(abs(V(:,j)-K+S)< eps_star, 1, 'last');
if ~isempty(index)
Sf(j) = S(index);
end
end
Now the value of S remains NaN, if no matching values are found.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by