matlab for loop function multiple output in matrix

China☆狼群 提交于 2019-12-12 01:43:05

问题


I am looking to create two separate matrices with a for loop over a function that creates two outputs. It looks somewhat like this:

% prepare output matrixes
output1=zeros(size(input2),1) % corresponds to a vector of length n of the nodes (bank capital
levels in this case)


output2=zeros(size(input2)) % corresponds to an adj matrix of size n

input2=[100,200,300 ... ] % corresponds to a vector of length n with capital levels.     
input1=1:length(input2); % correspounds to vector of [1:n] of individual banks to trigger each   
    bank's default in the model, so [1,2,3,4,5]

% input3 is an nxn adjacency matrix of directed exposures (with 0 along the
% main diagonal)
input3=[0,2190,7708,39034,32088,19584,18694,9923,513,14221; ... ]; %continues much longer ]
input4=0.8 % Loss Given Default (LGD
input5=0.4 % loss % of funding fraction from interbank lending
input6=0.4 % loss % from asset fire sales   

for j=1:input1
   [output1 output2] = function(input1, input2, input3, input4, input5,input6)
   output_matrix(j)=[output1(j) output2(j)]
end

Above is my failed attempt. Here is a manually created smaller version of the entire thing.

input3 = ...
 [70000, 15000, 24000, 52453; 
  23420, 24252, 10000, 35354; 
  98763, 45666, 96555, 05000; 
  09800, 54444, 04336, 67520]; % interbank loans in the function

input1 = 3; % default_bank

input2=[100000;200000;300000;400000]; % capital levels in the function
input4 = 1; 
input5 = .35; 
input6 = 1; 
% function calls on above inputs
[capital_losses defaulted_banks] = interbank_model( ...
input1, input2, input3, input4, input5, input6)

% this is the standard output for one iteration with default_bank=3, but I need this for 300+,   so a loop would be helpful... 

capital_losses3 =
1.0e+05 *
     0.5857
     0.2598
     3.0000
     0.0609


defaulted_banks3 =
     0 
     0
     1
     0

I would like to get the output for every defaulted bank, i.e. default_bank=1:4 to be displayed by the for loop as follows:

capital_losses_all =

   1.0e+05 *

    1.0000    0.2320    0.5857    0.5857
    0.2867    2.0000    0.2598    0.2598
    1.0716    0.4917    3.0000    3.0000
    0.2816    0.6682    0.0609    0.0609


 defaulted_banks_all =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

Every way I found to loop through the different banks that default, that is, through input 1:n, the old vector is overwritten n times by the previous one. I would like to generate a matrix of output1 where the iterations are concatenated for evaluation of each j and the same for output 2 for each value of j as shown in the 4x4 example above... I assume there is a simple answer and would be very thankful for any pointers. Thanks, Christoph

来源:https://stackoverflow.com/questions/23394737/matlab-for-loop-function-multiple-output-in-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!