# Generate all possible combinations of input vector Then store the input and outputs in each iteration in .mat file

2 views (last 30 days)
M on 4 Mar 2022
Commented: M on 4 Mar 2022
In the following code I want to generate all probability of being 0.5 in vector 'Pd' .
For example:
In the first iteration Pd will be as the following : Pd=[0.5 1 1 1 1 1 1]'
In the second iteration Pd will be as the following : Pd=[1 0.5 1 1 1 1 1]'
.
.
.
In i number of iteration Pd will be as the following : Pd=[1 0.5 0.5 0.5 1 1 1]'
In i number of iteration Pd will be as the following : Pd=[0.5 0.5 0.5 0.5 0.5 0.5 0.5]'
And so on ... (I want all possible combinations)
Then I want to store 'Pd' vector , 'Dd' matrix and 'w' vector in file which is named 'Comb#number of itertaion' (i.e , Comb1 , Comb2 ... ) in each iteration.
This is a part from the code:
Ph=[1 1 1 1 1 1 1]' ;
Pd=[0.5 1 1 1 1 1 1]' ;
Kd=zeros(14) ; Kh=zeros(14) ;
for i=1:7 ;
Ki=build_beam(i) ;
Kd=Kd+Pd(i)*Ki ;
Kh=Kh+Ph(i)*Ki ;
end ;
a=1:2:14;
r=7 ;
[Vd,Dd]=eig(Kd,M) ;
Dd=diag(Dd) ;
[Dd,I]=sort(Dd) ; Dd=Dd(1:vibmodes) ;
Vd=Vd(:,I); Td=Vd(:,1:vibmodes) ; Td=Td(a,:) ;
Dd=diag(Dd) ;
w=sqrt((diag(Dh)));

Matt J on 4 Mar 2022
Edited: Matt J on 4 Mar 2022
Well, here are all the combinations. You can loop through the rows of Pd and save them after whatever processing you wish. You've seen examples of similar things in previous posts. However, I think it's probably a bad idea to store the results to separate .mat files. You should probably store them all in one matfile as arrays.
[Pd{1:7}]=ndgrid([0.5,1]);
Pd=reshape( cat(8,Pd{:}) ,[],7)
Pd = 128×7
0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 1.0000 0.5000 0.5000 0.5000 0.5000 1.0000 1.0000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000
M on 4 Mar 2022
@Stephen Thank you, it is a good idea