フィルターのクリア

How do i select a random cell with a certain value from the matrix

4 ビュー (過去 30 日間)
Antonio Sarusic
Antonio Sarusic 2020 年 3 月 27 日
コメント済み: Adam Danz 2020 年 3 月 27 日
Hello,
I have a 1024x1280 matrix that only has the values 0 and 1.
Now I want to randomly select a cell that has the value 1.
Anybody got an idea how to do that?
Thanks

採用された回答

Adam Danz
Adam Danz 2020 年 3 月 27 日
編集済み: Adam Danz 2020 年 3 月 27 日
% Create matrix of 0s & 1s
M = round(rand(1024, 1280));
% List all indices of 1s
loc = find(M==1);
% Randomly select one of the indices
randInd = datasample(loc, 1); % or randInd = loc(randi(numel(loc)))
% Show the row and col number of the selection
[row, col] = ind2sub(size(M), randInd);
  2 件のコメント
Antonio Sarusic
Antonio Sarusic 2020 年 3 月 27 日
Thank you very much!
Adam Danz
Adam Danz 2020 年 3 月 27 日
Glad I could help!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by