constant randperm in each iteration

2 ビュー (過去 30 日間)
NA
NA 2019 年 2 月 13 日
コメント済み: Jan 2019 年 2 月 14 日
all=[2;3;4;56;7;89;5;1;5;1;3;6;7;9];
changed=9;
p = randperm(size(all,1));
p(1:changed)=[];
t=all/2
t(p)=0;
I want to run this code 10 times and want to have a same t in each iteration.
I tried to use 'seed' but have error.
  3 件のコメント
NA
NA 2019 年 2 月 14 日
all=[2;3;4;56;7;89;5;1;5;1;3;6;7;9];
changed=9;
p = randperm('seed',size(all,1));
p(1:changed)=[];
t=all/2
t(p)=0;
Error using randperm
Inputs must be nonnegative scalar integers.
Jan
Jan 2019 年 2 月 14 日
Please take the time to read the documentation if you have a problem with a command:
doc randperm
You cannot provide a 'seed' for this command. Trying this is pure guessing, and this must fail soon. Only the random number generator can be seeded.

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

採用された回答

Steven Lord
Steven Lord 2019 年 2 月 13 日
First, you should probably use a different variable name for your vector of data. all already has a meaning in MATLAB.
Second, define a fixed variable before whatever loop you're using to iterate then set t to that fixed variable at the start of each iteration.
fixedt = allVector/2;
for iterations = 1:10
t = fixedt;
% do stuff with t
end
  1 件のコメント
Jan
Jan 2019 年 2 月 14 日
編集済み: Jan 2019 年 2 月 14 日
@Naime: Of course. If you want a variable to have the same value for all iterations, define it outside the loop and do not change it inside.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by