loop through each row of data and insert data back & how to preallocate
Afficher commentaires plus anciens
I am returning about 13000 records from the db and then looping through each row to apply the bndprice and accrbond function. Then I am inserting back the 13000 rows. Question:
1) How do I preallocate the monthlyTotals cell array? w/o preallocating, it's taking about 10mins to calculate.
2) Is this the most efficient way of doing this (looping through record set and inserting results back?)
curs = fetch(curs);
[rows,cols]=size(curs.Data);
for i = 1:rows
price = bndprice( [curs.Data{i,2}], [curs.Data{i,3}], [curs.Data{i,5}], [curs.Data{i,6}], 'Basis', 1, 'DiscountBasis',1);
acc = acrubond([curs.Data{i,4}], [curs.Data{i,5}], [curs.Data{i,4}], 100, [curs.Data{i,3}], 2, 1);
trans = [curs.Data{i,1}];
% adds rows to monthlyTotals
monthlyTotals(i,:)=num2cell([price,acc,trans]);
end
colNames{1,1} = 'AmortizedCost';
colNames{1,2} = 'AccruedInterest';
colNames{1,3} = 'TransactionsId';
insert(conn,'DailyAccruedIncome',colNames,monthlyTotals)
Réponses (1)
Fangjun Jiang
le 19 Nov 2011
To pre-allocate, do this after you get the size:
monthlyTotals=cell(rows,1)
Then to do the assignment,
monthlyTotals(i)={[price,acc,trans]}
All the other places where you have [curs.Data{i,2}], [curs.Data{i,3}], you don't need the square bracket. That might save you some time too.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!