Moussekim
Moussekim

Reputation: 97

I want to select random sub matrix around value.

I want to value in random sub matrix..

For example.. First step, Select One random matrix(nxn) in matrix "A". (select search space..) Second step, Select middle value from this matrix.

also, Is it available?, first select "22" (It is random number in total matrix) , second find the matrix around "22".

I am rookie using MATLAB, Now I am studying hard about MATLAB. this is very interest! And I am sorry because I have very poor english ability..

Upvotes: 3

Views: 456

Answers (1)

For a matrix A, this code will randomly select a partition of size npart. While one could use randperm as in the original answer, @LuisMendo points out in comments that randi is simpler (and in fact it is faster as well).

npart = 3 ;
[m,n] = size(A) ;
ix = randi(m-npart+1) ;
iy = randi(n-npart+1) ;
X = A(ix:ix+npart-1,iy:iy+npart-1) ;

Of course the most efficient way to do this is to only sample a single random variable and convert the index back:

[m,n] = size(A) ;
ind = randi( (n-npart+1)*(m-npart+1) ) ;
[ix,iy] = ind2sub([m-npart+1,n-npart+1],ind) ;
X = A(ix:ix+npart-1,iy:iy+npart-1) ;

Upvotes: 3

Related Questions