could anyone help me to solve the issue with respect to the code
조회 수: 1 (최근 30일)
이전 댓글 표시
code:
A=1:7
B=length(A)
idx=randperm(B,1)
disp(idx)
the above code run one time and it displays idx value.
Could anyone help me to run the above code 5 times and for each time idx value should needs to be different.
댓글 수: 0
답변 (3개)
Soumya Sinha
2019년 6월 18일
I assume that for this case, since you only want idx value to change and not the values of A or B
A = 1:7;
B=length(A);
for i = 1:5
idx = randperm(B,1);
disp(idx);
end
댓글 수: 2
Soumya Sinha
2019년 6월 18일
That's a perfectly normal behaviour, you might get same numbers on some occasions, when I tried I got all different numbers in some cases and I few repeating. Moreover, randomness wouldn't ensure uniqueness in your answer.
Stephen23
2019년 6월 18일
"What i actually need is idx should be different for each run"
The easiest way is to define them before the loop, e.g.:
A = 1:7
N = numel(A)
V = randperm(B,N)
for k = 1:N
A(k)
V(k)
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!