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

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?

 Réponse acceptée

[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 commentaires

Hey, Thanks for the reply.
Hey, Here in my A matrix, it also contains lots of 0 values and while randomly selecting the i0 and j0, I got matrix of which have only 0 values. So, can I get rid of this 0 values while randomly selecting i0 and j0.
The code below will do that, by picking another random 11x11 matrix in case the one selected was all 0:
[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);
% counter to keep track of which slice we're trying to get
kk = 1;
while kk <= 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
new_slice = A(i0-min_row+1:i0+min_row-1,j0-min_col+1:j0+min_col-1);
% don't assign a new_slice that's all zeros to A_small ...
if all(new_slice(:) == 0)
continue % ... pick another random i0, j0 instead
end
% assign the valid new_slice to A_small:
A_small(:,:,kk) = new_slice;
% increment the counter:
kk = kk+1;
end
Thanks for reply,
I will let you know by trying this.
You're welcome! Yes, please let me know if it works, because I didn't run it (I don't have the tif image).

Connectez-vous pour commenter.

Plus de réponses (1)

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 commentaire

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

Connectez-vous pour commenter.

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by