Effacer les filtres
Effacer les filtres

How to generate random purmutation of string matrix ?

8 vues (au cours des 30 derniers jours)
Dimitar
Dimitar le 5 Déc 2014
Modifié(e) : Stephen23 le 5 Déc 2014
I would like to have binary mask matrix of sub-block matrices of 1s and 0s that are random inside the big matrix . The way i am trying to do it is to modify the matlab checkerboard function.
I would like to have random permutation of
tile = [white black; white black]
so that the checherboard matlab function is somewhat random each time when it is called
The code bellow does not work.
n = 10;
black = zeros(n);
white = ones(n);
tile = ([black white; white black ]);
P = perms(tile);
a = randi([1 24],1);
tile = P(a,:);
  2 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 5 Déc 2014
what is the string matrix? and what kind of permutation?
Dimitar
Dimitar le 5 Déc 2014
Modifié(e) : Dimitar le 5 Déc 2014
tile = ([black white; white black ]); string
I would like random out of the possible few... [white white ; black black] ...

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 5 Déc 2014
n = 2
black = zeros(n)
white = ones(n)
tile = {black white; white black }
idx = randperm(4)
tile=cell2mat(reshape(tile(idx),2,2))

Plus de réponses (1)

Stephen23
Stephen23 le 5 Déc 2014
Modifié(e) : Stephen23 le 5 Déc 2014
If you want to "randomize" the checkerboard pattern, then one way would be to use randperm to generate some random indices. This guarantees that the number of black and white elements is not randomly generated, but their distribution in the array is.
>> tile = {'white','black'};
>> reshape(tile(1+rem(randperm(9),2)),3,[])
ans =
'black' 'black' 'black'
'white' 'white' 'black'
'black' 'white' 'white'
In this case I chose 9 elements in a square, but you can change the inputs to randperm and reshape to generate whatever size and shape that you require.
Also note that the same method can be used for randomly assigning any two-element vector to a larger array: eg try tile = [1,0];, and that the order of reshape and tile is not important.

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by