MATLAB Answers

FIR

Selecting randomly from matrix

Asked by FIR
on 25 Jul 2012

I have a matrix B =

     2     5     6
     8     9    10
    56    89    10
     1     0     3
     8     1     3

in this i want to select a 2x2 matrix randomly ,please tell how to select

  2 Comments

Walter Roberson
on 25 Jul 2012

A 2x2 submatrix of adjacent values?

FIR
on 25 Jul 2012

Yes walter

Products

No products are associated with this question.

2 Answers

Answer by Andrei Bobrov
on 25 Jul 2012
Edited by Andrei Bobrov
on 25 Jul 2012

adjacent values

B(randi(size(B,1)-1)+(0:1),randi(size(B,2)-1)+(0:1));

not adjacent values

ii = randperm(size(B,1));
jj = randperm(size(B,2));
B(sort(ii(1:2)),sort(jj(1:2)))

  0 Comments


Thomas
Answer by Thomas
on 25 Jul 2012

perhaps something like this..

q=[  2     5     6
8     9    10
56    89    10
1     0     3
8     1     3]
row=randi(4);   %select start row
col=randi(2);   %select start colums
out=q(row:row+1,col:col+1)

  0 Comments


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today