Does anyone know what these lines of code are doing? Thanks

11 vues (au cours des 30 derniers jours)
Ifeatu Ezenwe
Ifeatu Ezenwe le 13 Déc 2018
Commenté : Adam Danz le 13 Déc 2018
ma=RandChooseN(probs,M); %function probs with the argument m
pa=RandChooseN(probs,M);
xp=ceil(rand(1,M)*(Nt-1));
pop(1:2:popsiz*e,:)=[pop(ma,1:xp(1)) pop(pa,xp(1)+1:Nt)];
pop(2:2:popsize,:)=[pop(pa,1:xp(1)) pop(ma,xp(1)+1:Nt)];

Réponses (1)

Adam Danz
Adam Danz le 13 Déc 2018
Modifié(e) : Adam Danz le 13 Déc 2018
"Does anyone know what these lines of code are doing? "
The short answer: no. No, because we can't be sure what RandChooseN() is doing; We don't know the values of probs, M, Nt, pop, popsiz, or e.
However, we can speculate to a certain point.
Assuming probs is a vector of probabilities and M is an integer, and assuming RandChooseN() is this function (<- link), then ma and pa are vectors of random integers chosen randomly with probabilites listed in probs.
ma=RandChooseN(probs,M); %function probs with the argument m
pa=RandChooseN(probs,M);
Along with the assumptions above, if Nt is a scalar (single number), xp is vector of rounded, integers the same size as M. If Nt is a column vector and M is a row vector (or vise-versa) then xp will be a matrix of rounded, integers. Based on the how Nt is used in the next few lines, I'm guessing it's a scalar and xp is a vector.
xp=ceil(rand(1,M)*(Nt-1));
These next lines look questionable. pop is probably a 2D matrix. Data is being put into the odd numered rows of pop and then different data is being put into the even numered rows. I don't know why the odd rows extend to popsiz*e but the even rows only extend to popsize. The data being put into the pop matrix is also taken from the pop matrix, so the data is being reorganized and some of it, overwritten. The random probability integers taken from the first 2 lines of code above, ma and pa, are used to select the rows of pop.
pop(1:2:popsiz*e,:)=[pop(ma,1:xp(1)) pop(pa,xp(1)+1:Nt)];
pop(2:2:popsize,:)=[pop(pa,1:xp(1)) pop(ma,xp(1)+1:Nt)];
There's not enough context to be able to speculate higher level explanations. If I had to throw a blind dart, I'd say that you have a matrix 'pop' and you're randomly choosing rows according to a probability distribution and shuffling those rows. The columns look like they are subsampled as well.
  2 commentaires
Ifeatu Ezenwe
Ifeatu Ezenwe le 13 Déc 2018
Thanks alot for your answer, this the code that comes before the code I asked above.
Would this help? The comments are my understanding of what the code is doing.
ff = inline('sum(x,2)');
maxit=200; %number of iterations/generations
maxcost=9999999;
popsize=100;
mutrate=0.001;
nbits=20;
npar=1;
Nt=nbits*npar;
iga=0;
pop=round(rand(popsize,Nt)); %round to the nearest decimal or integer
cost=feval(ff,pop); %evaluate function ff
%with the argument pop
%assigns it to the variable 'cost'
[cost,ind]=sort(cost,'descend'); %sorts 'cost' in descending order
%gets the associated indices into the original vector
pop=pop(ind,:); %sorts population with max cost first
%uses those indices to re-define pop
%sorting it according to cost
maxc(1)=max(cost);
meanc(1)=mean(cost);
probs=cost/sum(cost);
while iga<maxit
iga=iga+1;
M=ceil(popsize/2);
Adam Danz
Adam Danz le 13 Déc 2018
Here's my recommendations.
1) Step through the code, line by line, and understand each line. This can be done in debug mode (<- link).
2) for each function that you are unfamiliar with, read the documentation. Use the help or doc command. For example,
help round %to see how the round() function works
doc round
3) when you get stuck, come back here and ask a more specific question.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices 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!

Translated by