For loop with random value to select from an array

8 次查看(过去 30 天)
Hi.. I would like to select random values each iteration (some of them will be repeated )how could I do that ?
The output should be something like the following: [5 2 3 1 1 3 4]
clearvars;
clc
y=[1 5 4 3 2];
for i=1:8
new_y=y(randperm(i))
end
  1 个评论
Rik
Rik 2020-8-28
In the future please respond with a comment instead of a new question. You essentially asked the same question half an hour ago.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2020-8-28
If you only want a single value from your array I would suggest this code:
clearvars;
clc
y=[1 5 4 3 2];
for i=1:8
new_y=y(randi(end));
%
%code that uses the new_y scalar
%
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by