For loop with random value to select from an array

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 comentario

Rik
Rik el 28 de Ag. de 2020
In the future please respond with a comment instead of a new question. You essentially asked the same question half an hour ago.

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 28 de Ag. de 2020
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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 28 de Ag. de 2020

Comentada:

Rik
el 28 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by