subscripted assignment dimension mismatch - Edge effect

3 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2015-10-14
编辑:Stephen23 2015-10-14
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

更多回答(1 个)

Walter Roberson
Walter Roberson 2015-10-14
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 个评论
Rdmato33
Rdmato33 2015-10-14
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by