could anyone help me to solve the issue with respect to the code

1 回表示 (過去 30 日間)
jaah navi
jaah navi 2019 年 6 月 18 日
回答済み: Stephen23 2019 年 6 月 18 日
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.

回答 (3 件)

James Tursa
James Tursa 2019 年 6 月 18 日
The syntax of a for-loop is:
for k=1:5
% your code here
end
  1 件のコメント
jaah navi
jaah navi 2019 年 6 月 18 日
code:
for k=1:5
A=1:7;
B=length(A);
idx=randperm(B,1);
disp(idx)
end
when i run the code it gives the same idx for some run.
what i actually need is it should be different for each run.

サインインしてコメントする。


Soumya Sinha
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 件のコメント
jaah navi
jaah navi 2019 年 6 月 18 日
ok.But the idx remains the same for more than one run when i run the code
idx = 1
idx = 1
idx = 1
idx = 7
idx = 4
What i actually need is idx should be different for each run.But with respect to the above result idx remains the same for three run .Could you please help me on this.
Soumya Sinha
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
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

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by