How to generate random purmutation of string matrix ?
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Azzi Abdelmalek
2014-12-5
n = 2
black = zeros(n)
white = ones(n)
tile = {black white; white black }
idx = randperm(4)
tile=cell2mat(reshape(tile(idx),2,2))
0 个评论
更多回答(1 个)
Stephen23
2014-12-5
编辑:Stephen23
2014-12-5
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!