subscripted assignment dimension mismatch - Edge effect

1 visualización (últimos 30 días)
Rdmato33
Rdmato33 el 14 de Oct. de 2015
Comentada: Walter Roberson el 14 de Oct. de 2015
Hello, I am trying to create 92 samples of 5x5 cells from an image. Problem : the loop is stopped by this message : subscripted assignment dimension mismatch. This is due to the edge effect (see below). How can I launch the loop without problem? How can I adapt the matrix size located to the edge?
This is my code :
for m=1:92
a(:,:,m) = svf(xy_points(m,1)-2:xy_points(m,1) + 2, xy_points(m,2)-2:xy_points(m,2)+2);
end
Thanks.

Respuesta aceptada

Stephen23
Stephen23 el 14 de Oct. de 2015
Editada: Stephen23 el 14 de Oct. de 2015
Note that whatever method you choose you will have a different number of elements in each sample, which means you will need to revise your sample storage strategy. One option is a cell array, another would be to preallocate a numeric array and assign only as many value as the sample contains.
Method One: Use Logical Indexing
Create vectors of permitted X and Y indices, and compare these with the indices that you generate in each loop. Use the logical comparison to select that actual sampled area. Here is a simple demonstration of this:
data = reshape(0:19,[],4)';
row_domain = 1:size(data,1);
col_domain = 1:size(data,2);
% Start loop here!
% Sample indices (here some are outside of the matrix)
row_sample = 3:7;
col_sample = 4:9;
%
row_idx = any(bsxfun(@eq,row_sample(:),row_domain),1);
col_idx = any(bsxfun(@eq,col_sample(:),col_domain),1);
%
mat_sample = data(row_idx,col_idx)
% End loop here!
This sampled without error one corner of the data matrix, even though the requested indices went outside the bounds of the data matrix:
>> data
data =
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
>> mat_sample
mat_sample =
13 14
18 19
Method Two: Use min and max
% Start loop here!
% Sample indices (here some are outside of the matrix)
row_beg = 3;
row_end = 7;
col_beg = 4;
col_end = 9;
row_idy = max(1,row_beg):min(size(data,1),row_end);
col_idy = max(1,col_beg):min(size(data,2),col_end);
mat_out = data(row_idy,col_idy);
% End loop here!
Produes this output:
>> mat_out
mat_out =
13 14
18 19

Más respuestas (1)

Walter Roberson
Walter Roberson el 14 de Oct. de 2015
L = max(1, xy_points(:,1)-2);
R = min(xy_points(:,1)+2, size(svf,2));
T = max(1, x_points(:,2)-2);
B = min(xy_points(:,2)+2, size(svf, 1));
for m=1:92
a{m} = svf( L(m):R(m), T(m):B(m) );
end
A cell array has to be used because the cells can end up different sizes due to the clipping against the boundaries.
  3 comentarios
Rdmato33
Rdmato33 el 14 de Oct. de 2015
It works!! :) I just changed the order for R and B : size(svf,2)/size(svf,1) to size(svf,1)/size(svf,2) Thanks a lot!
Walter Roberson
Walter Roberson el 14 de Oct. de 2015
Ah yes, I did switch them. Should have been
svf( T(m):B(m), L(m):R(m) )

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by