Below is my code for a neural network Forward propagation. I want to speed it up. As for loop takes time, Can any body help in correcting the code for speeding it up, like matlab says vectorzing etc. In this code i take receptive field of 4x4 each time from input of size 19x19, than multiply each pixel with 4x4 of weights (net.w{layer_no}(u,v) of size 19x19). You can also say it is a dot product of the two. I didnt did directly dot product of two small matrices as there is a check of boundaries. It provides a 6x6 output saved in output in the end. I am not an experienced coder, so i did as much as i can. Can anybody guide me how to speed it up as it takes alot of time compare to Opencv. Will be thankful. Regards
    receptiveSize = 4;
    overlap= 1;
    inhibatory = 0;
    gap = receptiveSize-overlap;
    UpperLayerSize = size(net.b{layer_no}); % 6x6
    Curr_layerSize = size(net.w{layer_no}); % 19x19
    for u=1:UpperLayerSize(1)-1
        for v=1:UpperLayerSize(2)-1
            summed_value=0;
            min_u = (u - 1) * gap + 1;
            max_u = (u - 1) * gap + receptiveSize;
            min_v = (v - 1) * gap + 1;
            max_v = (v - 1) * gap + receptiveSize;
            for i = min_u : max_u
                for j = min_v : max_v
                    if(i>Curr_layerSize(1) || j>Curr_layerSize(2))
                        continue;
                    end
                    if(i<1 || j<1)
                        continue;
                    end
                    summed_value = summed_value + input{layer_no}.images(i,j,sample_ind) * net.w{layer_no}(i,j);
                 end
            end
            summed_value = summed_value + net.b{layer_no}(u,v);
            input{layer_no+1}.images(u,v,sample_ind) = summed_value;
        end
    end
    temp = activate_Mat(input{layer_no+1}.images(:,:,sample_ind),net.AF{layer_no});
    output{layer_no}.images(:,:,sample_ind) = temp(:,:);
 
    