MATLAB: Selecting a random element from a matrix with a range.

matricesrandom

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.

Best Answer

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