필터 지우기
필터 지우기

How to generate random purmutation of string matrix ?

조회 수: 8 (최근 30일)
Dimitar
Dimitar 2014년 12월 5일
편집: Stephen23 2014년 12월 5일
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
Azzi Abdelmalek 2014년 12월 5일
what is the string matrix? and what kind of permutation?
Dimitar
Dimitar 2014년 12월 5일
편집: Dimitar 2014년 12월 5일
tile = ([black white; white black ]); string
I would like random out of the possible few... [white white ; black black] ...

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
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))

추가 답변 (1개)

Stephen23
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.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by