Hi Jake, You have the right idea but there need to be some changes. First of all, you have
but that would set your row indices to the values contained in the a matrix. The row indices should be directly the indices:
Once that change is made, further on you are trying to increment an index ii that is already specified by a for loop. Take a for loop that goes something like
for q = 5:7; do stuff; q = q+3; end
The for loop is going run three times and set the value of q to 5,6, and 7. Incrementing q inside does not do anything (unless you use q further along inside the loop). You need to use something like a 'while' loop, which lets you run the loop with incremented values . The while loop can also incorporate the conditionals you have on jj and kk. Putting that all together gives
a = [1 2 3 4 5 6; 1 2 3 5 7 6; 3 4 5 6 7 8; 4 5 6 7 8 9; 1 2 3 1 2 3; 1 4 5 6 7 8];
c = a;
kernel = (1/16).*[1 2 1;2 4 2;1 2 1];
[row,col]= size(a);
ii= 1; jj= 3; yy= 2;
while jj<= row
mm= 1; kk= 3; tt= 2;
while kk<=col
result = a(ii:jj,mm:kk).* kernel;
b = sum(sum(result));
c(yy,tt)= b;
mm= mm+1; kk= kk+1; tt= tt+1;
end
ii= ii+1; jj= jj+1; yy= yy+1;
end
disp(c)
disp(conv2(a,kernel,'valid'))
Also, this code creates 'c' as a copy of 'a' and makes the changes on it, leaving 'a' alone. If you change 'a' as you go, on your next step you will start averaging in values that have already been changed from the previous step. mean2 has been replaced by a straight sum since the kernel is doing a specified weighed average already. Row and column indices in 'c' are swapped.
You are correctly incrementing ii, jj etc. When you set them up initially, ii=1, yy=2, jj=3. So yy=ii+1, jj=ii+2, but since you increment all three of them together, that's always true. You could replace yy by ii+1 and jj by ii+2 everywhere in the code and just keep and increment ii. Same with the other three indices.
Best Answer