creating a max-pooling layer using nested loop.

1 view (last 30 days)
I am trying to make a max pooling layer with a 3x3 filter and stride 3. I'm trying to only use for loops for this code to work. here is my code.
main = [1 2 3 4 5 6;
7 8 9 10 11 12;
13 14 15 16 17 18;
19 20 21 22 23 24;
25 26 27 28 29 30;
31 32 33 34 35 36];
temp=0;
%temporary var for stride
tmpX=0;
tmpY=0;
for x=1:size(main,1)/3
for y=1:size(main,2)/3
for cx=1:3
for cy=1:3
%stride by 3
cy = cy+tmpY;
cx = cx+tmpX;
if main(cx,cy)>temp
temp = main(cx,cy)
end
end
end
if y == size(main,2)/3
tmpY=0;
tmpX=tmpX+3;
else
tmpY=tmpY+3;
end
end
end
I'm using the matrix 'main' as a sample input for the pooling, the program should give a 2x2 matrix output of [15,18,33,36] but I'm getting [15,18,19,0] ad an error message that says 'Index in position 1 exceeds array bounds (must not exceed 6).'

Answers (1)

Image Analyst
Image Analyst on 28 Dec 2018
Just look at what the variables are. cx is 7 and main is only 6 wide.
By the way, matrices have the horizontal (column) index second, not first like you have it. So matrices are main(y, x) NOT main(x, y).

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!