Azzera filtri
Azzera filtri

How to generate random purmutation of string matrix ?

7 visualizzazioni (ultimi 30 giorni)
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 Commenti
Azzi Abdelmalek
Azzi Abdelmalek il 5 Dic 2014
what is the string matrix? and what kind of permutation?
Dimitar
Dimitar il 5 Dic 2014
Modificato: Dimitar il 5 Dic 2014
tile = ([black white; white black ]); string
I would like random out of the possible few... [white white ; black black] ...

Accedi per commentare.

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 5 Dic 2014
n = 2
black = zeros(n)
white = ones(n)
tile = {black white; white black }
idx = randperm(4)
tile=cell2mat(reshape(tile(idx),2,2))

Più risposte (1)

Stephen23
Stephen23 il 5 Dic 2014
Modificato: Stephen23 il 5 Dic 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.

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by