help I have 40 iteration , 10 iteration of 40 at random to do something and the other 30 iteration for do another thing??
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
maha ismail
am 19 Jan. 2015
Kommentiert: Star Strider
am 19 Jan. 2015
help I have 40 iteration , 10 iteration of 40 at random to do something and the other 30 iteration for do another thing?? for t=1:40 take 10 of 40 at random to do something and other 30 for do another thing
0 Kommentare
Akzeptierte Antwort
Star Strider
am 19 Jan. 2015
If you want to choose 10 iterations randomly out of a total of 40, this is one way:
ran10 = randperm(40, 10); % Choose 10 Random Iterations From 40
for k1 = 1:40
if any(ran10 == k1)
fprintf('\n\tDo something randomly at k1 = %d\n', k1)
else
fprintf('\n\tDo something else at k1 = %d\n', k1)
end
end
2 Kommentare
Star Strider
am 19 Jan. 2015
I don’t know what 6.5 had, but I assume the problem is with randperm.
This restatement of ran10 should work:
ran10 = fix(40*rand(10,1));
(If this solves your problem, please Accept my Answer.)
Weitere Antworten (1)
Marcel
am 19 Jan. 2015
Don't quite understand the question. Can't you just create 2 loops?
For i=1:30
<stuff happens>
end
For i=31:40
<other stuff happens>
end
2 Kommentare
Stephen23
am 19 Jan. 2015
@ Marcel: you should avoid using i or j as the loop variables, as these are the names of the inbuilt imaginary unit .
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!