How to make a matrix only with 2 types of numbers randomly

Hello,
It may sound a silly question but i cannot think in the desired solution. I need to make a matriz 1:24 only with values that might be 5 or 10? How can i make it randomly?
Example of the desired solution: [10 5 10 5 5 5 10 10 5 10 10 10 10 10 10 5 5 5 5 5 10 5 10 10]

 Réponse acceptée

Wayne King
Wayne King le 18 Déc 2012
Modifié(e) : Wayne King le 18 Déc 2012
x = rand(24,1);
y = zeros(size(x));
y(x<0.5) = 10;
y(x>0.5) = 5;
The above gives you 10's and 5's occurring with equal probability.

1 commentaire

Jan
Jan le 18 Déc 2012
Modifié(e) : Jan le 18 Déc 2012
Faster and avoid the (rare) bug when x==0.5:
x = rand(24,1);
y = repmat(5, size(x));
y(x<0.5) = 10;

Connectez-vous pour commenter.

Plus de réponses (1)

Here's another way:
a = 5 * (randi(2, 1, 24)-1) + 5

3 commentaires

Image Analyst
Image Analyst le 18 Déc 2012
Modifié(e) : Image Analyst le 18 Déc 2012
I hope this wasn't your homework! You didn't tag it as such. If it were I would have just given you a hint to use randi().
André Pacheco
André Pacheco le 18 Déc 2012
Modifié(e) : André Pacheco le 18 Déc 2012
Thanks it worked ;) How about if i wanted to have numbers 3 types of numbers? -100, 0 and 100?
Oh nvm, i picked up in ur solution and adapt it to my situation. so it would come: a = (randi(3, 1, 24)-2)*100
Thanks a lot ;)
If you have the Image Processing Toolbox you can use the new imquantize() function.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by