Info

This question is closed. Reopen it to edit or answer.

How do I make a table of a value with its coordinate ung nested for loop?

1 view (last 30 days)
In this code, the Inew is the sum of a window of an image and i intended to store its value to 3 column of matrix with its coordinate in the image but the value of i and j stored in the matrix is wrong, the j is a constant value and the i is also wrong.
for i=1:1: size(I,1) - 31
for j=1:size(I,2) - 31
Inew(i,j) = sum(sum(I(i:i+31,j:j+31)));
sort_mat(i,1:3) = [i,j,Inew(i,j)];
end
end
Help please Thank you!

Answers (2)

Image Analyst
Image Analyst on 28 Sep 2013
Edited: Image Analyst on 28 Sep 2013
I don't know why you need the row and column. Just put the sum in a new image and you have everything you need.
sumImage = conv2(yourImage, ones(31), 'same');
That format you were suggesting is probably not good for anything. I just don't see why you need it. If you do, you can use meshgrid() and then tack on the sumImage as the third column as I calculated above, but again, I don't think you will need that.

Azzi Abdelmalek
Azzi Abdelmalek on 28 Sep 2013
Edited: Azzi Abdelmalek on 28 Sep 2013
Your code is wrong. You can notice that j is constant because sort_mat(i,1:3) is erasing itself until j=size(I,2)-31. Your second column is equal to this value. It should be
ii=0;
for i=1:1: size(I,1) - 31
for j=1:size(I,2) - 31
ii=ii+1;
Inew(i,j) = sum(sum(I(i:i+31,j:j+31)));
sort_mat(ii,1:3) = [i,j,Inew(i,j)];
end
end
It's also recommended to pre-allocate sort_mat before the for loop

Community Treasure Hunt

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

Start Hunting!