i have a greyscale image of size 512x512 and i have divided into 4x4 non overlapping blocks so i get 16384 blocks to process, by processing it i get the last blocks only.

2 views (last 30 days)
my code is
X=mat2cell(I1,4*ones(1,128),4*ones(1,128));
for i=1:16384
A = X{i}(:);
Di = abs(diff(A));
end
here i got the all the block but when the loops end and processing is finish it return only the last block.

Answers (1)

Guillaume
Guillaume on 1 May 2017
Well, in the expression Di = ... matlab doesn't know that you want to replace the i by whichever value i is at the moment. So obviously, you're overwriting the variable Di at each step of the loop. The fix is to use indexing: D(i) = .... To avoid resizing the destination array each time you add an element, you should predeclare it before the loop with D = zeros(numel(X)), resulting in:
X = mat2cell(I1, 4*ones(1, 128), 4*ones(1, 128));
D = zeros(numel(X));
for i = 1:numel(X) %don't hardcode sizes when you can just query them
A = X{I}(:);
D(i) = abs(diff(A));
end
Or... you could use blockproc which does the separating into block, looping and creating the destination array all for you:
D = blockproc(I1, [4, 4], @(block) abs(diff(block.data(:))));
  2 Comments
KSSV
KSSV on 1 May 2017
I1 = rand(512,512) ;
X=mat2cell(I1,4*ones(1,128),4*ones(1,128));
Di = zeros(numel(X),15) ;
for i=1:16384
A = X{i}(:);
Di(i,:) = abs(diff(A));
end
Guillaume
Guillaume on 1 May 2017
Oh, yes of course, the result of the diff is a vector. Thanks for the correction.
As said, using blockproc would mean not having to worry about creating a destination the correct size.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!