Vector from loop into matrix and optimization
1 view (last 30 days)
Show older comments
Hello, I would like to put the results of ddd into a matrix results_vec(z,:)=ddd. So far, only the last vector of ddd is pasted into results_vec(z,:). However, I would like to have all z=3 vectors of ddd into results_vec matrix.
My actual goal is to optimize S_vec. For that reason I will use the matrix results_vec and sort for the first row so that I can find the appropriate values for C vector. Are there any other approaches available?
Thanks for help!
%Given:
Yn; % 1 x 90 vector with integers between 0 and 1
R1; % 90 x 1 vector with integers between 0 and -1
size_vec; %90 x 1 vector an/E1
D=exposures-E;% 1 x 90 vector with integers above 0
E; % 1 x 90 vector with integers above 0
% Optimize x1:x90:
%C=[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64 x65 x66 x67 x68 x69 x70 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80 x81 x82 x83 x84 x85 x86 x87 x88 x89 x90]
% x1:x90 are the values we want to optimize! Thus, unknown yet. xi>0;
%sum(C)=200000; %Restriction
%Minimize Target function S_vec
z=3;
results_vec=zeros(z,91);
for i=1:z
C=randfixedsum(90,1,200000,0,200000);
%creates a 90 x 1 vector with random integers btw. 0 and 200000.
%restriction: sum of integers= 200000;
B_vec=diag(zeros(1,90));
for i=1:90
bn=(D(1,i)-C(i,1))/(E(1,i)+C(i,1));
B_vec(i,i)=bn;
end
B_vec(B_vec>30)=30;
S_vec = Yn*diag(size_vec)*diag(assetsalesf(B_vec,M,F1)); % 1 x 90 vec
%Optimize S_vec!
ddd=[sum(S_vec) transpose(C)];
results_vec(z,:)=ddd
end
% table_results_opti=table(resultsss_vec)
% filename = 'optimi_matlab.xlsx';
% writetable(table_results_opti,filename,'Sheet',1,'Range','A1');
%
% winopen('optimi_matlab.xlsx')
0 Comments
Answers (3)
Guillaume
on 11 May 2018
Edited: Guillaume
on 11 May 2018
There are several problem with your code. In my opinion the biggest is that you don't use meaningful variable names and hence end up using the wrong variable.
The second problem is that you have a for loop with index i (meaningless variable name) and inside that loop you have another loop with the same index variable i.
The third problem is that you never actually use the loop index to put the ddd (extremely useless variable name!) into results_vec. Instead you use z which is a constant you set to 3. So, every time you write the data to row 3. Note that you couldn't use the row index i of the outer for loop since you've overwritten it with the i of the inner for loop.
Anyway:
numrows = 3; %much better name than z
results_vec = zeros(numrows, 91);
for row = 1:numrows %better name than i
%....
for diagindex = 1:90
%....
end
%...
please_give_me_a_meaningful_name = [sum(S_vec), transpose(C)]; %your horribly named ddd
results_vec(row, :) = please_give_me_a_meaningful_name;
end
%...
Note that I haven't really tried to understand what your code is doing, but it looks to me that the inner loop could be removed entirely and the diagonal calculated in one go:
B_vec = diag((D(1, :) - C(:, 1).') ./ (E(1, :) + C(:, 1).'));
And what a misleading name that B_vec is since it's actually a square matrix!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!