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
7 次查看(过去 30 天)
显示 更早的评论
ai = randi(8,10,1);
0 个评论
采纳的回答
Ameer Hamza
2020-11-2
编辑:Ameer Hamza
2020-11-2
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)
更多回答(1 个)
Johannes Hougaard
2020-11-2
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,'.')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!