I cant CREATE array the rand() in for loop..... Firstly, generate 72 bit binary number in 20 times. After, i need use the second, tenth or fifth generated random binary number.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mahmut Yasak
am 6 Jan. 2016
Kommentiert: Mahmut Yasak
am 6 Jan. 2016
But, error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ga_function2 (line 9)
random_chromosome(popSize) = round(rand(1, 72));
random_chromosome = [1;20];
for popSize = 1 : 1 : 20
random_chromosome(popSize) = round(rand(1, 72));
end
0 Kommentare
Akzeptierte Antwort
Stephen23
am 6 Jan. 2016
Bearbeitet: Stephen23
am 6 Jan. 2016
Because popSize is scalar, the code
random_chromosome(popSize)
refers to one element of the array random_chromosome, but rand(1, 72) is an array with 72 elements.
You are trying to fit seventy-two elements into the space of one element in a matrix, which obviously does not fit and causes the error. One better solution would be to avoid that inefficient loop altogether:
X = randi(2,20,72)-1;
produces a matrix X of size 20x72, where each row corresponds to one of your "random binary number". For example the second number is simply the second row:
X(2,:)
You can learn more about basic MATLAB usage by doing these tutorials:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!