For loop with random value to select from an array

조회 수: 2 (최근 30일)
Ali Tawfik
Ali Tawfik 2020년 8월 28일
댓글: Rik 2020년 8월 28일
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