How to generate random purmutation of string matrix ?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dimitar
am 5 Dez. 2014
Bearbeitet: Stephen23
am 5 Dez. 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 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 5 Dez. 2014
n = 2
black = zeros(n)
white = ones(n)
tile = {black white; white black }
idx = randperm(4)
tile=cell2mat(reshape(tile(idx),2,2))
0 Kommentare
Weitere Antworten (1)
Stephen23
am 5 Dez. 2014
Bearbeitet: Stephen23
am 5 Dez. 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.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!