Filter löschen
Filter löschen

For loop with random value to select from an array

10 Ansichten (letzte 30 Tage)
Ali Tawfik
Ali Tawfik am 28 Aug. 2020
Kommentiert: Rik am 28 Aug. 2020
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 Kommentar
Rik
Rik am 28 Aug. 2020
In the future please respond with a comment instead of a new question. You essentially asked the same question half an hour ago.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 28 Aug. 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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by