How to reset a FOR loop if condition is met?

1 回表示 (過去 30 日間)
Geoffrey
Geoffrey 2014 年 4 月 17 日
回答済み: per isakson 2014 年 4 月 17 日
Hello,
I am running a for loop that generates two random values that correspond to the randomly generated indices of a matrix. I want to check the matrix at those coordinates to see whether or not the cell is empty. If it isn't empty, I want to regenerate a pair of random coordinates. I'm not sure the best or most efficient way of doing this. I have something that looks like this:
randX = randi([1,size(emptyGrid,2)]); % random coordinates
randY = randi([1,size(emptyGrid,1)]);
if emptyGrid(randX,randY) ~= 0;
???
end % check cell
Any help or insight would be appreciated!
Thank you,
Geoff

回答 (1 件)

per isakson
per isakson 2014 年 4 月 17 日
  • "most efficient way" . See Premature optimization
  • Why not show us a solution with a for-loop that does what you want.
  • "to regenerate a pair of random coordinates" . Those new pairs what shall they be used for?

カテゴリ

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