Alternative to while loops without using loop?

Hi, are there any more efficient alternatives to while loops. eg
a = zeros(3,1,3);
b = ones(3,1,3);
while isequal(a,b) == 0
a(:,:,1) = [round(rand);round(rand);round(rand)];
a(:,:,2) = [round(rand);round(rand);round(rand)];
a(:,:,3) = [round(rand);round(rand);round(rand)];
b(:,:,1) = [round(rand);round(rand);round(rand)];
b(:,:,2) = [round(rand);round(rand);round(rand)];
b(:,:,3) = [round(rand);round(rand);round(rand)];
end
I

回答 (2 件)

the cyclist
the cyclist 2018 年 9 月 5 日

0 投票

a = zeros(3,1,3);
b = ones(3,1,3);
while not(isequal(a,b))
a = round(rand(3,1,3));
b = round(rand(3,1,3));
end

4 件のコメント

Manne Plok
Manne Plok 2018 年 9 月 5 日
Hi, while that is a much more concise answer, I was looking for a way to remove the while loop entirely. Is that possible? Thanks.
the cyclist
the cyclist 2018 年 9 月 5 日
Conceptually, the while loop seems fine here.
If a and b are randomly generated, they could be the same (although it is not likely). And that could happen a bunch of times in a row (again, not likely).
But you do need to safeguard against those unlikely events, and the while loop seems to be a completely intuitive way to do that. Why are you trying to get rid of it?
All that being said, there is another way, but it actually seems more cumbersome to me. There is a finite number (call it N) of pairs of 3x1x3 arrays of zeros and ones (where you exclude identical pairs). You could just create the list of those, and order them somehow. Then just randomly choose a number from 1:N, and pick the corresponding pair of matrices.
the cyclist
the cyclist 2018 年 9 月 5 日
Separate non sequitur remark ... if you have the Statistics and Machine Learning Toolbox, you could use the binornd command instead of round(rand()) to generate zeros and ones. I'm not sure which would be more computationally efficient, but I think what you are doing would be clearer.
Steven Lord
Steven Lord 2018 年 9 月 5 日
Another alternative would be to use randi specifying [0 1] as the first input.

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

the cyclist
the cyclist 2018 年 9 月 5 日
編集済み: the cyclist 2018 年 9 月 5 日

0 投票

Here is a way to do this without a while loop. It uses the trick I mentioned in a comment, that there are a finite number (512) ways of filling a 3x1x3 array with zeros and ones, and therefore there are 512x511 = 261,632 ways of choosing a pair of distinct entries.
In the code, I ...
  • create the full list
  • pick a at random
  • delete that choice from the list
  • pick b at random
list = dec2bin(0:511) - '0';
a_idx = randi(512);
a = reshape(list(a_idx,:),3,1,3);
list(a_idx,:) = [];
b_idx = randi(511);
b = reshape(list(b_idx,:),3,1,3);
The very first line is itself a pretty slick trick for getting all the permutations.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2017b

質問済み:

2018 年 9 月 5 日

編集済み:

2018 年 9 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by