While loop and randomization

1 回表示 (過去 30 日間)
Marie Marie
Marie Marie 2021 年 6 月 6 日
コメント済み: Jan 2021 年 6 月 7 日
Thank you in advance for your help!
if (valCount == ones)
disp(y1)
else
disp('repetition')
end
% this if/end works manually, but it's not a while loop, so it doesn't work
% automatically...
  1 件のコメント
Jan
Jan 2021 年 6 月 7 日
Unfortuantely you have removed important parts of your question. Please don't do this. The nature of this forum is sharing problems and solution. The voluntary users spend time to help you and they want the thread to be useful for others also.

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

回答 (1 件)

Jan
Jan 2021 年 6 月 6 日
編集済み: Jan 2021 年 6 月 6 日
x = [1 1 1 2 2 2 3 3 3];
ready = false;
while ~ready
y1 = [x(randperm(numel(x))); x(randperm(numel(x))); x(randperm(numel(x)))];
ready = numel(unique(y1(:, 1))) == size(y1, 1);
end
Instead of a rejection you can use an constructive method:
x = [1 1 1 2 2 2 3 3 3];
nx = numel(x);
ux = unique(x);
nRow = 3;
y1 = zeros(nRow, nx);
for k = 1:nRow
v = x(randperm(nx));
v(find(v == ux(k), 1)) = [];
y1(k, :) = [ux(k), v];
end
  1 件のコメント
Marie Marie
Marie Marie 2021 年 6 月 6 日
thank you!

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

カテゴリ

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