bilal
bilal

Reputation: 5

Generate a random matrix in MATLAB with equal number of values less than 0.5 and greater than 0.5

I want to generate a random matrix of size n such that it has equal number of elements less than 0.5 and greater than 0.5

Upvotes: 0

Views: 149

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

The following will create a matrix with first half of numbers less than 0.5 and next half of numbers greater than 0.5 :-

required= [0.5*rand(n,n/2),  0.5+rand(n,n/2)]; 

EDIT :- As now you also mentioned that you want shuffled numbers, add the following in your code too:-

required=reshape(required(randperm(numel(required))),size(required))

Please also note that this is only possible when n is an even integer because half of an even integer is also an integer whereas half of an odd integer can never be an integer!

Upvotes: 3

Related Questions