Selecting a random element from a matrix with a range.

1 回表示 (過去 30 日間)
Shane McNamara
Shane McNamara 2017 年 10 月 23 日
コメント済み: Shane McNamara 2017 年 10 月 24 日
I am trying to select a random element from this matrix with a certain range.
d = rand(10)
d(end,randperm(size(d,2), 1))
This creates a 10*10 matrix between 1 and 0. I want to slect a random variable < 0.5 but i am trouble applying this to the code. Any help is much appreciated.
  2 件のコメント
Cedric
Cedric 2017 年 10 月 23 日
There may not be any element < 0.5. How do you define the number of elements that you will pick? What is the purpose?
Shane McNamara
Shane McNamara 2017 年 10 月 24 日
Thanks for your feedback Cedric. There usually is values above and below .5. I only want to pick one element which is what the above does. I wil apply it in the future to a matrix of zeros and 1's and apply a random movement to the element picked.

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

採用された回答

Akira Agata
Akira Agata 2017 年 10 月 24 日
How about the following script?
d = rand(10);
% Extract elements of d < 0.5
idx = d < 0.5;
d2 = d(idx);
% If d2 is NOT empty, select one element randomly
if ~isempty(d2)
output = d2(randperm(numel(d2),1));
end
  1 件のコメント
Shane McNamara
Shane McNamara 2017 年 10 月 24 日

Hi Akira thanks a million! This works perfectly.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by