help I have 40 iteration , 10 iteration of 40 at random to do something and the other 30 iteration for do another thing??

조회 수: 3 (최근 30일)
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

채택된 답변

Star Strider
Star Strider 2015년 1월 19일
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
maha ismail
maha ismail 2015년 1월 19일
this is what I need but rewrite this code in a simple code because I work In matlab version 6.5 year2000
Star Strider
Star Strider 2015년 1월 19일
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.)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Marcel
Marcel 2015년 1월 19일
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
maha ismail
maha ismail 2015년 1월 19일
I mean the I is not in sequence 10 iteration but not in order may 1 6 8 11 30 25 13 7 5 22 this 10 iteration in random but the other 30 its the other numbers don't selected in the 10 iteration
Stephen23
Stephen23 2015년 1월 19일
@ Marcel: you should avoid using i or j as the loop variables, as these are the names of the inbuilt imaginary unit .

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by