How can I randomly select 1000 small matrix like 11X11 from 7081X7811 matrix size?

1 view (last 30 days)
I have 7081X7811 size of matrix which have image pixel values, want to randomly select small matrix of size 11X11.
Right now, I am reading one small matrix like:
[A, R] = geotiffread('...tif'); %reading .tif image
%(200,1414) this center for below matrix
i0 = 200;
j0 = 1414; %center of small 11X11 matrix
[row, col] = size(A);
A_small = A((i0-5):(i0+5),(j0-5):(j0+5));
can you guys please help me in this?

Accepted Answer

Voss
Voss on 15 Mar 2022
[A, R] = geotiffread('...tif'); %reading .tif image
[row, col] = size(A); % row = 7081, col = 7811
N_small = 1000;
small_size = [11 11];
min_row = (small_size(1)+1)/2; % min_row = 6
max_row = row-min_row+1; % max_row = 7076
min_col = (small_size(2)+1)/2; % min_col = 6
max_col = col-min_col+1; % max_col = 7806
% A_small is a matrix of size 11-by-11-by-1000, each slice of
% which will be a random 11-by-11 selection from A:
A_small = zeros(small_size(1),small_size(2),N_small);
for kk = 1:N_small
% max_row-min_row+1 = 7071
% -> randi returns a random integer between 1 and 7071
% -> i0 is a random integer between 6 and 7076
i0 = randi(max_row-min_row+1)+min_row-1;
% max_col-min_col+1 = 7801
% -> randi returns a random integer between 1 and 7801
% -> j0 is a random integer between 6 and 7806
j0 = randi(max_col-min_col+1)+min_col-1;
% select from A the 11-by-11 matrix centered at i0,j0
% and assign it to the kk-th slice of A_small
A_small(:,:,kk) = A(i0-min_row+1:i0+min_row-1,j0-min_col+1:j0+min_col-1);
end
  6 Comments
Voss
Voss on 18 Mar 2022
You're welcome! Yes, please let me know if it works, because I didn't run it (I don't have the tif image).

Sign in to comment.

More Answers (1)

David Hill
David Hill on 15 Mar 2022
r=randi(7070,1000,1);
c=randi(7800,1000,1);
for k=1:1000
newMatrix(:,:,k)=yourMatrix(r(k):r(k)+11,c(k):c(k)+11);
end
  1 Comment
Torsten
Torsten on 15 Mar 2022
I think
r=randi(7071,1000,1);
c=randi(7801,1000,1);
for k=1:1000
newMatrix(:,:,k)=yourMatrix(r(k):r(k)+10,c(k):c(k)+10);
end
instead of
r=randi(7070,1000,1);
c=randi(7800,1000,1);
for k=1:1000
newMatrix(:,:,k)=yourMatrix(r(k):r(k)+11,c(k):c(k)+11);
end

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!