Placing Blocks of fixed size to random positions in a bigger matrix

조회 수: 1 (최근 30일)
How do you place a smaller matrix(block) of fixed size in a bigger matrix at random positions?
  댓글 수: 1
Bharathwaj
Bharathwaj 2011년 11월 25일
what if the placement of the smaller blocks should not overlap with the existing blocks ?

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 11월 16일
M = zeros(5)
m = randi(4,3)
sM = size(M);
sm = size(m);
k=sM-sm+1;
iM = reshape(1:numel(M),sM(1),[]);
iw = iM(1:k(1),1:k(2));
im = bsxfun(@plus,(0:sm(1)-1).',0:sM(1):sM(1)*(sm(2)-1));
M(iw(randi(numel(m)))+im) = m

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 16일
a=rand(3);
A=magic(10);
[m,n]=size(a);
[M,N]=size(A);
i=floor((M-m)*rand)+1;
j=floor((N-n)*rand)+1;
A(i+1:i+m,j+1:j+n)=a
  댓글 수: 1
Bharathwaj
Bharathwaj 2011년 11월 25일
what if the placement of the smaller blocks should not overlap with the existing blocks ?

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

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by