Save a vector into matrix in a for loop

3 views (last 30 days)
Daniel Arvidsson
Daniel Arvidsson on 6 Apr 2017
Answered: Zanjesh Kapoor on 11 May 2018
Hello!
The result from the code in the for loop is a 3x1 matrix, or, it doesnt work but the code T_matrix(Layup(i,5))'*e(i) are giving me that.
But how do i save that result in the matrix e_local??? For example, i want to save each iteration i in a new column. So every column in my matrix e_local will have 3 rows. The bold part in the code below is where i dont know what to write to achieve that, thanks!
e_local=zeros(3,1);
for i=1:size(Layup)
e_local (i,i:3)=e_local+T_matrix(Layup(i,5))'*e(i)
end

Answers (2)

KSSV
KSSV on 6 Apr 2017
N = size(Layup) ;
e_local=zeros(3,N);
for i=1:N
e_local (:,i)=e_local+T_matrix(Layup(i,5))'*e(i) ;
end
  2 Comments
Daniel Arvidsson
Daniel Arvidsson on 6 Apr 2017
Edited: Daniel Arvidsson on 6 Apr 2017
Hey!
Thanks, but this was actually what i was looking for :)
e_local=zeros(3,1);
for i=1:size(Layup)
e_local(:,i)=[T_matrix(Layup(i,5))'*e(i)] ;
end
e_local =
3.6450 3.5196 1.9774 0.4367 3.1434 3.0181
5.0589 3.5182 3.3942 3.2688 -1.1041 -2.6448
-0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000
KSSV
KSSV on 6 Apr 2017
In the code I posted, I have initialized e_local. This makes code to run faster. Consider using the code which I gave. And thank is accepting the answer.

Sign in to comment.


Zanjesh Kapoor
Zanjesh Kapoor on 11 May 2018
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); sum_vec_sum=zeros(5,1); 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')

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!