I have a matrix of values as given below.
candidate_edges =
10.0000 10.0000 6.0000
155.5000 105.5000 75.5000
25.5000 105.5000 75.5000
295.5000 415.5000 155.5000
185.5000 415.5000 155.5000
185.5000 485.5000 155.5000
195.5000 305.5000 74.5000
115.5000 305.5000 74.5000
115.5000 395.5000 74.5000
195.5000 395.5000 74.5000
25.5000 185.5000 75.5000
155.5000 185.5000 75.5000
295.5000 415.5000 5.5000
295.5000 415.5000 155.5000
185.5000 415.5000 155.5000
195.5000 305.5000 5.5000
195.5000 305.5000 74.5000
195.5000 395.5000 74.5000
295.5000 415.5000 5.5000
195.5000 395.5000 5.5000
295.5000 485.5000 5.5000
300.0000 600.0000 0
295.5000 415.5000 155.5000
185.5000 415.5000 155.5000
I want to generate an array with length 1: length(candidate_edges) and in each cell, I want to store each row of the above matrix. Below is my code
cnode = zeros(1, n);
cnode = cell(1, n);
for k = 1:length(cnode)
for j = 1:length(candidate_edges)
cnode{k} = candidate_edges(j,:);
end
end
In the output, I get only same value in each cell.
You do not need two loops. e.g. this should do
cnode = cell(1, size(candidate_edges, 1))
for k = 1:numel(cnode)
cnode{k} = candidate_edges(k,:);
end
You can also use the inbuilt MATLAB function mat2cell for the same like this:
cnode = mat2cell(candidate_edges, ones(1, size(candidate_edges, 1)));
Related
I am trying to stacked two column charts that have positive and negative values on top of each other. But with the following codes I get the column charts separate and sometimes one is hidden behind the other. I am posting the script and a sample data so that it can be replicable. I am using online Matlab
retndecm(1:52,1)= [0.3715
0.3590
0.2862
0.1936
0.2177
0.2111
0.2019
0.2012
0.2162
0.2236
0.1874
0.2116
0.2569
0.2384
0.2436
0.2416
0.2590
0.2682
0.2695
0.2832
0.2616
0.2506
0.2548
0.2470
0.2612
0.2588
0.2399
0.2848
0.3023
0.3166
0.3535
0.3328
0.3187
0.3365
0.3313
0.3417
0.3411
0.4013
0.3717
0.3574
0.3288
0.3071
0.3271
0.2802
0.2787
0.2649
0.2619
0.2258
0.2432
0.2314
0.2151
0.2080];
retndecm(1:52,2)=[0.0561
0.0462
0.0783
0.1246
0.1099
0.0921
0.0755
0.0675
0.0582
0.0599
0.0249
0.0137
0.0136
0.0108
0.0052
0.0176
0.0259
0.0183
0.0258
0.0312
0.0383
0.0421
0.0293
0.0376
0.0355
0.0344
0.0341
0.0321
0.0404
0.0289
0.0318
0.0374
0.0283
0.0289
0.0405
0.0321
0.0402
0.0528
0.0791
0.0756
0.0798
0.0892
0.0933
0.0978
0.1073
0.1194
0.1172
0.1117
0.1174
0.0960
0.1041
0.1178];
retndecm(1:52,3) = [-0.1152
-0.0963
-0.1035
-0.1144
-0.1309
-0.1289
-0.1337
-0.1347
-0.1272
-0.1289
-0.1361
-0.1176
-0.1057
-0.1125
-0.1068
-0.1119
-0.1142
-0.1188
-0.1163
-0.1115
-0.1098
-0.1145
-0.1110
-0.1046
-0.0953
-0.0985
-0.1044
-0.0967
-0.0964
-0.0932
-0.0959
-0.0938
-0.0930
-0.0843
-0.0815
-0.0673
-0.0609
-0.0591
-0.0464
-0.0736
-0.0681
-0.0691
-0.0612
-0.0758
-0.0658
-0.0690
-0.0736
-0.0774
-0.0731
-0.0762
-0.0711
-0.0754];
ndfspi(1:52,1)= [0.6025
0.6024
0.6028
0.6024
0.6030
0.6028
0.6026
0.6031
0.6031
0.6031
0.6029
0.6029
0.6010
0.6030
0.6033
0.6025
0.6034
0.6021
0.6014
0.6027
0.6031
0.6032
0.6030
0.6032
0.6029
0.6025
0.6024
0.6029
0.6026
0.6011
0.6025
0.6013
0.6025
0.6028
0.5989
0.6025
0.6028
0.6035
0.6035
0.6033
0.6031
0.6030
0.6033
0.6027
0.6030
0.6031
0.6033
0.6029
0.6031
0.6027
0.6026
0.6029];
nfundbeta = ndfspi(1:52,1)-1;
%% figures
set(groot,'DefaultFigureColormap',jet);
r=figure;
hold on;
M=retndecm(:,1)';
t = datetime(2001,1,5) + calweeks(1:52);
h = bar(t, retndecm(:,1),.99,"stacked");
set(h, 'FaceColor', 'flat');
h.CData = ndfspi(:,1)';
caxis([min(-1) max(1)]);
hold on;
t = datetime(2001,1,5) + calweeks(1:52);
l=bar(t,retndecm(:,2),.99,"stacked");
l.FaceColor = [.5 0 .5];
hold on;
N=retndecm(:,3)';
t = datetime(2001,1,5) + calweeks(1:52);
h = bar(t, retndecm(:,3),.99,"stacked");
set(h, 'FaceColor', 'flat');
h.CData = nfundbeta(:,1)';
caxis([min(-1) max(1)]);
colorbar('eastoutside');
set(gcf, 'PaperSize', [20 10], 'PaperPosition', [0 0 20 10])
set(findall(gcf,'-property','FontSize'),'FontSize',12);
saveas(gcf,'test2', 'pdf');
hold off;
I have essentially copied the code from the Matlab Example file BermudanSwaption.m in an effort to calibrate the Hull White one factor model to some given market data. As I use a subset about 30 or fewer swaptions for my calibration set, I am able to arrive at an interior solution. But when I use a larger set of instruments, I get a weird error:
Below is the code:
ValuationDate = '10-01-2014';
Settle = datenum(ValuationDate);
% Zero rate data is market data, bootstrapped from Bloomberg and Reuters quotes
CurveDates = [735874;
735882;
735906;
735936;
735950;
736040;
736133;
736224;
736314;
736424;
736606;
736788;
736971;
737153;
737336;
737518;
737701;
737884;
738069;
738251;
738433;
738615;
738797;
738979;
739162;
739345;
739528;
739710;
739893;
740075;
740260;
740442;
740624;
740806;
740989;
741171;
741354;
741536;
741719;
741901;
742084;
742269;
742451;
742633;
742815;
742997;
743180;
743362;
743545;
743728;
743911;
744093;
744278;
744460;
744642;
744824;
745006;
745189;
745372;
745554;
745737;
745919;
746102;
746284;
746469;
746651;
746833;
747015;
747198;
747380;
747563;
747745;
747928;
748111;
748296;
748478;
748660;
748842;
749024;
749206;
749389;
749572;
749755;
749937;
750120;
750302;
750487];
ZeroRates = 1.0e-03*[0.0172;
0.0188;
0.0191;
0.0221;
0.0249;
0.0244;
0.0269;
0.0333;
0.0423;
0.0571;
0.0789;
0.1021;
0.1253;
0.1435;
0.1617;
0.1749;
0.1881;
0.1973;
0.2064;
0.2158;
0.2253;
0.2311;
0.2370;
0.2429;
0.2488;
0.2547;
0.2607;
0.2640;
0.2672;
0.2706;
0.2738;
0.2772;
0.2807;
0.2842;
0.2877;
0.2913;
0.2948;
0.2964;
0.2979;
0.2995;
0.3011;
0.3026;
0.3043;
0.3060;
0.3077;
0.3095;
0.3112;
0.3118;
0.3125;
0.3132;
0.3138;
0.3146;
0.3152;
0.3160;
0.3167;
0.3175;
0.3183;
0.3186;
0.3189;
0.3192;
0.3196;
0.3199;
0.3202;
0.3206;
0.3209;
0.3213;
0.3217;
0.3217;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3217;
0.3217;
0.3218;
0.3218;
0.3219;
0.3219;
0.3220;
0.3220;
0.3221;
0.3221];
Compounding = 2;
RateSpec = intenvset('Compounding', 2,'ValuationDate', ValuationDate,'StartDates', ValuationDate,'EndDates', CurveDates,'Rates', ZeroRates);
InstrumentMaturity = datenum('12-Sep-2044');
% Swaption Vol data from Bloomberg
SwaptionBlackVol = [ 0.5940 0.5550 0.4450 0.3710 0.3400 0.3110 0.2910 0.2750 0.2630 0.2520 0.2250 0.2140 0.2080 0.2050;
0.5630 0.5470 0.4420 0.3690 0.3360 0.3090 0.2900 0.2740 0.2630 0.2520 0.2260 0.2150 0.2090 0.2060;
0.5760 0.5330 0.4400 0.3730 0.3410 0.3150 0.2970 0.2820 0.2700 0.2590 0.2330 0.2220 0.2170 0.2140;
0.5840 0.5020 0.4240 0.3730 0.3480 0.3240 0.3060 0.2920 0.2810 0.2710 0.2430 0.2300 0.2230 0.2190;
0.5630 0.4750 0.4100 0.3700 0.3450 0.3230 0.3070 0.2940 0.2830 0.2740 0.2470 0.2330 0.2260 0.2210;
0.5510 0.4520 0.3980 0.3660 0.3410 0.3220 0.3070 0.2950 0.2850 0.2760 0.2500 0.2360 0.2290 0.2240;
0.4630 0.4010 0.3660 0.3440 0.3250 0.3100 0.2990 0.2890 0.2790 0.2720 0.2470 0.2320 0.2260 0.2210;
0.4230 0.3750 0.3480 0.3290 0.3140 0.3030 0.2930 0.2840 0.2760 0.2690 0.2420 0.2300 0.2240 0.2190;
0.3700 0.3470 0.3280 0.3110 0.2960 0.2880 0.2800 0.2730 0.2680 0.2620 0.2360 0.2240 0.2190 0.2150;
0.3420 0.3250 0.3100 0.2970 0.2850 0.2770 0.2700 0.2640 0.2590 0.2540 0.2280 0.2180 0.2140 0.2110;
0.3230 0.3010 0.2900 0.2810 0.2720 0.2650 0.2590 0.2540 0.2500 0.2470 0.2230 0.2130 0.2090 0.2060;
0.3010 0.2860 0.2760 0.2670 0.2580 0.2530 0.2480 0.2450 0.2420 0.2390 0.2160 0.2060 0.2030 0.2000;
0.2850 0.2750 0.2650 0.2560 0.2480 0.2440 0.2400 0.2370 0.2350 0.2320 0.2100 0.2000 0.1970 0.1940;
0.2710 0.2600 0.2510 0.2440 0.2380 0.2340 0.2310 0.2290 0.2260 0.2240 0.2040 0.1940 0.1910 0.1890;
0.2580 0.2470 0.2400 0.2350 0.2300 0.2270 0.2240 0.2210 0.2190 0.2170 0.1980 0.1890 0.1860 0.1840;
0.2460 0.2370 0.2320 0.2270 0.2240 0.2210 0.2180 0.2150 0.2130 0.2110 0.1980 0.1840 0.1820 0.1800;
0.2040 0.1980 0.1950 0.1920 0.1900 0.1890 0.1890 0.1880 0.1880 0.1870 0.1720 0.1660 0.1640 0.1620;
0.1790 0.1750 0.1740 0.1730 0.1730 0.1710 0.1710 0.1700 0.1690 0.1690 0.1530 0.1510 0.1500 0.1480;
0.1650 0.1650 0.1660 0.1670 0.1680 0.1670 0.1670 0.1680 0.1680 0.1680 0.1550 0.1580 0.1560 0.1530;
0.1530 0.1570 0.1590 0.1620 0.1640 0.1650 0.1660 0.1670 0.1680 0.1690 0.1560 0.1650 0.1620 0.1590];
% The tenors for the underlying swaps and the options on them
SwaptionExerciseDates = cellstr(['1M ';'2M ';'3M '; '6M ';'9M ';'1Y ';'18M';'2Y ';'3Y ';'4Y ';'5Y ';'6Y ';'7Y ';'8Y ';'9Y ';'10Y';'15Y';'20Y';'25Y';'30Y']);
SwaptionTenors = cellstr(['1Y ';
'2Y ';
'3Y ';
'4Y ';
'5Y ';
'6Y ';
'7Y ';
'8Y ';
'9Y ';
'10Y';
'15Y';
'20Y';
'25Y';
'30Y']);
testmat = zeros(length(SwaptionExerciseDates),1);
% Here I construct a matrix of exercise dates
for i = 1:length(SwaptionExerciseDates)
if SwaptionExerciseDates{i}(end)=='Y'
testmat(i) = addtodate(Settle,str2double(SwaptionExerciseDates{i}(1:end-1)),'year');
elseif SwaptionExerciseDates{i}(end)=='M'
testmat(i)=addtodate(Settle,str2double(SwaptionExerciseDates{i}(1:end-1)),'month');
end
end
EurExDates= testmat;
EurExDatesFull = repmat(testmat,1,length(SwaptionTenors));
testmat2 = zeros(length(SwaptionExerciseDates),length(SwaptionTenors));
% Here I construct a matix of maturity dates
for i = 1:size(EurExDatesFull,1)
for j = 1:size(EurExDatesFull,2)
if SwaptionTenors{j}(end)=='Y'
testmat2(i,j) = addtodate(EurExDatesFull(i,j),str2double(SwaptionTenors{j}(1:end-1)),'year');
elseif SwaptionTenors{j}(end)=='M'
testmat2(i,j)= addtodate(EurExDatesFull(i,j),str2double(SwaptionTenors{j}(1:end-1)),'month');
end
end
end
EurMatFull = testmat2;
% I construct an index of all the swaptions that I intend to use for calibration
relidx = find(EurMatFull <= InstrumentMaturity);
SwaptionBlackPrices = zeros(size(SwaptionBlackVol));
SwaptionStrike = zeros(size(SwaptionBlackVol));
% back out the swaption strikes and prices from the implied vol data.
for iSwaption=1:length(SwaptionExerciseDates)
for iTenor=1:length(SwaptionTenors)
[~,SwaptionStrike(iSwaption,iTenor)] = swapbyzero(RateSpec,[NaN 0],Settle, EurMatFull(iSwaption,iTenor),...
'StartDate',EurExDatesFull(iSwaption,iTenor),'LegReset',[1 2],'Basis',2);
SwaptionBlackPrices(iSwaption,iTenor) = swaptionbyblk(RateSpec,'call', SwaptionStrike(iSwaption,iTenor),Settle, ...
EurExDatesFull(iSwaption,iTenor), EurMatFull(iSwaption,iTenor),SwaptionBlackVol(iSwaption,iTenor));
end
end
TimeSpec = hwtimespec(Settle,daysadd(Settle,30*(1:370),6), 12);
% construct an index of some random collection of instruments
B = (214:224);
HW1Fobjfun4 = #(x) SwaptionBlackPrices(relidx(B)) - ...
swaptionbyhw(hwtree(hwvolspec(ValuationDate,testmat,x(2),testmat,x(1),'spline'), RateSpec, TimeSpec), 'call',SwaptionStrike(relidx(B)),EurExDatesFull(relidx(B)), 0,EurExDatesFull(relidx(B)), EurMatFull(relidx(B)),'Basis',2, 'SwapReset',12);
options = optimset('disp','iter','MaxFunEvals',1000,'TolFun',1e-5);
x0 = [.1 .01];
lb = [0 0];
ub = [1 1];
HW1Fparams = lsqnonlin(HW1Fobjfun4,x0,lb,ub,options)
In the results, when I just use instruments numbered 214:224, I am able to find a local minima: HW1Fparams(1) = 0.0801 , HW1Fparams(2) = 0.0002
however when I use instruments say 150:224 I get the following error:
"subscript indices must be real positive integers or logicals" error in cummswapcfbytrintree (line 23)
This is a Matlab created file. Does anyone know what's going wrong here? How do I go about diagnosing/debugging in the error is in some Matlab created file. The usual techniques of checking which variable or which index is being given a zero or double value are not at my disposal anymore. Thanks.
I try to import data from a text file to MATLAB, which has the following structure:
** Porosity
**
*POR *ALL
0.1500 0.0900 2*0.1300 0.1400 4*0.1500 0.2200 2*0.1500 0.0500
0.0900 0.1400 5*0.1500 0.2300 0.2600 0.0800 0.1500 0.1500 0.2400 0.1700
[...]
The header has to be ignored obviously. Space is the delimiter, while * indicates that the same value occurs several times as indicated by the integer before the *.
Unfortunately, the number of entries per line varies. Ideally I want to store all values in one array like this:
por = [0.1500 0.0900 0.1300 0.1300 0.1400 0.1500 0.1500 0.1500 0.1500 0.1500 0.2200 0.1500 0.1500 ...]
Can this be solved with the textscan command somehow? The file is rather large with some hundred thousand values, so I need a quick solution ;) Help is greatly appreciated!
Straight forward way (I did not use Matlab for a long period of time, so it might be not the best solution)
fid = fopen('temp.txt');
data = textscan(fid, '%s', 'delimiter', ' ');
fclose(fid);
out = convert_cells(data);
And function
function out = convert_cells(cells)
out = [];
for i = 1 : size(cells{1})
tmp = strsplit(cells{1}{i}, '*');
num1 = str2double(tmp(1));
if size(tmp, 2) == 2 && ~isnan(num1)
num2 = str2double(tmp(2));
if ~isnan(num2)
out = [out repmat(num2, 1, num1)];
end;
elseif size(tmp, 2) == 1 && ~isnan(num1)
out(end + 1) = num1;
end;
end;
end
I wanted to be:
arr(2,1) = arr(2,1) + abs(5.0 minus 5.1);% where I(1,a) is 5.0 and I(1,a+1)is 5.1
Why couldn't I add the below two together? I got this error message:
Subscripted assignment dimension mismatch (size [1 x 1] ~= size [1 x :?]).
Function 'MATLAB Function' (#620.435.485), line 23, column 2:
"arr(count,1) = arr(count,1) + abs(I(1,a)-I(1,a+1))"
Launch diagnostic report.
'I' is declared in worskspace as:
I =
5.0000 5.1000 5.2000 5.2000
90.0000 85.0000 80.0000 20.0000
integ_signal=zeros(5,4);
a=zeros(10);
arr=zeros(5,4);
count=2;
a=1;
integ_signal(count,1)= integ_signal(count-1,1);
arr(count,1) = integ_signal(count,1);
arr(count,1) = arr(count,1) + abs(I(1,a)-I(1,a+1));
The system is shown here. The complete code is below:
function integ_signal= fcn(I,V,count,oldval)
integ_signal=zeros(5,1);
a=zeros(10);
arr=zeros(2,4);
%b=zeros(10);
integ_signal=oldval;
coder.extrinsic('load');
load('data.mat','I');
a=1;
%b=1;
if count==1
integ_signal(count,1) = 10; % Initial Condition
end
if count>1
integ_signal(count,1)= integ_signal(count-1,1);
arr(count,1) = integ_signal(count,1);
arr(count,1) = arr(count,1) + abs(I(1,a)-I(1,a+1));
end
a=a+1;
% b++;
end
Dear Sir, I keep getting this error, my desired output is to get abs(I(2,ii(a))-I(2,ii(a)-1) so that when ii(a)=2, I want I(2,2) minus I(2,1) which is 5.1 minus 5.0 : Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.
"2"
. at this line:
arr(1,ii(a)) = arr(1,ii(a)) + abs(I(2,ii(a))-I(2,ii(a)-1));.
I define I as the following in .mat file:
save data -v7.3 'I'
load('data.mat','I');
I =
Columns 1 through 6
0 1.0000 2.0000 3.0000 4.0000 5.0000
5.0000 5.1000 5.2000 5.2000 5.5000 5.9000
Column 7
6.0000
6.0000
I guess you call your code with count as a vector. Also, it seems like you're missing a loop. (If not, why the a = a + 1?)
a = zeros(10);
followed by
a = 1;
does not make much sense. What is your input variable I? What do you want load('data.mat','I') to do?
If I'm correct, try something like:
for ii = 1:length(count)
if count(ii) == 1
integ_signal(count(ii),1) = 10; % Initial Condition
end
if count(ii) > 1
integ_signal(count(ii),1)= integ_signal(count(ii)-1,1); % Or, (count(ii-1),1)
arr(count(ii),1) = integ_signal(count(ii),1);
arr(count(ii),1) = arr(count(ii),1) + abs(I(1,a)-I(1,a+1));
end
a = a + 1;
end
You probably need to tweak this in some way, but I think it may help you a bit.
I keep getting this error:
Index expression out of bounds. Attempted to access element 2. The
valid range is 1-1."2".
My desired output is to get
abs(I(2,ii(a))-I(2,ii(a)-1)
so that when ii(a)=2, I want I(2,2) minus I(2,1) which is 5.1 minus 5.0
The error is due to this line:
arr(1,ii(a)) = arr(1,ii(a)) + abs(I(2,ii(a))-I(2,ii(a)-1));.
I define I as the following in .mat file:
save data -v7.3 'I'
load('data.mat','I');
I =
Columns 1 through 6
0 1.0000 2.0000 3.0000 4.0000
5.0000 5.1000 5.2000 5.2000 5.5000
The code:
function arr= fcn(I,count,oldval)
persistent integ_signal
if isempty( integ_signal)
integ_signal=zeros(1,5)
end
persistent a
if isempty(a)
a=zeros(1)
end
arr=zeros(1,5);
ii=zeros(1,5);
aa=zeros(2,5);
integ_signal=oldval;
coder.extrinsic('load');
aa=load('data.mat','I');
if count==1
a=1;
ii(a)=count;
integ_signal(1,ii(a)) = 10; % Initial Condition
end
if count ~= 1
a=count;
ii(a)=count;
integ_signal(1,ii(a))= integ_signal(1, ii(a)-1);
arr(1,ii(a)) = integ_signal(1,ii(a));
arr(1,ii(a)) = arr(1,ii(a)) + abs(aa(2,ii(a))-aa(2,ii(a)-1));
end
a = a + 1;
end