how to generate N times many of line code 'for loop'?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, i need some guidance for this type of problem. here's the detail:
as we can see in code line; i have three types of force in the shape of matrix, there are:
a. Increase Loop System (first and second force loop system)
b. Decrease Loop System (first and second force loop system)
there are 2 force loop systems
small notes: input is manually write down in excel.
For first loop; cosist of:
A. Force Loop Increase
- matxfl_inc1 = empty matrix for put the value [ zeros(max(big_dof),1)]
- td_f1inc = search the 'length', for looping, input from excel
- fdofl_1inc = for putting excatly the force position to
- floop_1inc = forces
- matxfl_inc1 = put all forces into here
- afl_inc1= to describe simply, it's almost same for matxfl_inc1, but we only take some of matxfl_inc1, [matxfl_inc1(a_dof,1)]
b. Force Loop Decrease (more/less is same from above, but different name and different place of excel to get called)
- matxfl_dec1 = empty matrix for put the value [ zeros(max(big_dof),1)]
- td_f1dec = search the 'length' for looping, input from excel
- fdofl_1dec = for putting excatly the force position to
- floop_1dec = forces
- matxfl_dec1 = put all forces into here
- afl_dec1= to describe simply, it's almost same for matxfl_inc1, but we only take some of matxfl_inc1, [matxfl_dec1(a_dof,1)]
For Henceforth of loop, it is basically the same, but with different name (matxfl_inc2, td_f2inc, ..., matxfl_dec2, td_f2dec, ... , matxfl_inc3, and so on) and also more/less is same from above, but different name and different place of excel to get called)
for input is called from excel (in my code is data2, at sheet 2), with details below
- first column (assume at A3 like my input in excel) for how much looping of variety, ex: in need 7 varieties, so i want to generate 7 of both increase and decrease force loop system
- from B3 to K3 ( 2 to 11) in excel is for first loop;
- from L3 to U3 (12 to 21) in excel is for second loop
for details for code we can see the line code below
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% First Force Loop System
% Loop Increase Forces Placement
matxfl_inc1 = zeros(max(big_dof),1); %empty matx for added looping force
td_f1inc = length(rmmissing((data2(:,2))));
for u=1:td_f1inc
fdofl_1inc = data2(u,2); %fdof_l= force loop dofs being read
floop_1inc = data2(u,3); %force_loop= added forces for loop
matxfl_inc1(fdofl_1inc,1) = matxfl_inc1(fdofl_1inc,1) + floop_1inc;
end
afl_inc1=matxfl_inc1(a_dof,1); %defined added loop force ...
%at active dofs
% Loop Decrease Force Placements
matxfl_dec1 = zeros(max(big_dof),1); %empty matx for added looping force
td_f1dec = length(rmmissing((data2(:,4))));
for u=1:td_f1dec
fdofl_1dec = data2(u,4); %fdof_l= force loop dofs being read
floop_1dec = data2(u,5); %force_loop= added forces for loop
matxfl_dec1(fdofl_1dec,1) = matxfl_dec1(fdofl_1dec,1) + floop_1dec;
end
afl_dec1=matxfl_dec1(a_dof,1); %defined added loop force...
%at active dofs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Second Force Loop System
% Loop Increase Forces Placement
matxfl_inc2 = zeros(max(big_dof),1); %empty matx for added looping force
td_f2inc = length(rmmissing((data2(:,12))));
for u=1:td_f2inc
fdofl_2inc = data2(u,12); %fdof_l= force loop dofs being read
floop_2inc = data2(u,13); %force_loop= added forces for loop
matxfl_inc2(fdofl_2inc,1) = matxfl_inc2(fdofl_2inc,1) + floop_2inc;
end
afl_inc2=matxfl_inc2(a_dof,1); %defined added loop force ...
%at active dofs
% Loop Decrease Force Placements
matxfl_dec2 = zeros(max(big_dof),1); %empty matx for added looping force
td_f2dec = length(rmmissing((data2(:,14))));
for u=1:td_f2dec
fdofl_2dec = data2(u,14); %fdof_l= force loop dofs being read
floop_2dec = data2(u,15); %force_loop= added forces for loop
matxfl_dec2(fdofl_2dec,1) = matxfl_dec2(fdofl_2dec,1) + floop_2dec;
end
afl_dec2=matxfl_dec2(a_dof,1); %defined added loop force...
%at active dofs
what i have to create is N-Loop System that consists of N-times Increase Loop System, and N-times Decrease Loop System. assume that i want to have 7 varies of Loop System (Inc and Dec), so i have to generate 7 for each of them. dont forget that each N-Loop System have a different input. ex: ' fdofl_1dec = data2(u,4); ' and ' fdofl_2dec = data2(u,14) ' . data2(u,1) is for knowing how my force variety i want to do ( N, example N=7)
notes: i create this with input from excel with some benefits. if i want my first loop system have 50 forces of increase and decrease, i just write it in column, so to make N-Loop system write sidewise is a must
full code you can see at the attachments
thank, much love.
5 commentaires
Jan
le 3 Juil 2022
No, do not create variables by hiding an index in their names, but use arrays:
matxfl_inc1 % Bad
matxfl_inc{1} % Good
matxfl_inc(:, :, 1); % Good, or how many dimensions you need
If all arrays have the same size, use a mutli-dimensional array, a cell array otherwise.
complicated methods to hide an index in the name of a variable demands for creating even more complicated methods to access them later on. Using indices is trivial and easy to expand.
Réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!