I have to generate an array of 10x1 in an ascending order. With a condition that the two consecutive elements should have a difference between 1 to 8
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ahmad Javaid
am 2 Nov. 2020
Kommentiert: Ahmad Javaid
am 2 Nov. 2020
ai = randi(8,10,1);
0 Kommentare
Akzeptierte Antwort
Ameer Hamza
am 2 Nov. 2020
Bearbeitet: Ameer Hamza
am 2 Nov. 2020
Following fulfills the conditions
x = 1:10
If you want to use randi() then do something like this
dx = randi(8,10,1);
x = cumsum(dx)
Weitere Antworten (1)
Johannes Hougaard
am 2 Nov. 2020
This is not a super elegant solution but I think it'll do the trick for you
numbers = zeros(10,1);
numbers(1) = randi(8,1); %If 8 should be the maximum number you would start
for ii = 2:10
numbers(ii) = numbers(ii-1) + round(rand*7+1)
end
figure;plot(numbers,'.')
Siehe auch
Kategorien
Mehr zu Genetic Algorithm finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!