Using Vectorization to Calculate New Values Based on Surrounding Values

Pardon any mistakes, I'm going through a massive panic attack at the moment with the stress of trying to solve this the last 4+ hours.
I have a project for my programming class that wants me to write a bunch of functions to assist an edge finding function. It has to all be vectorized, ie no iteration or selection. And I'm failing to vectorize the very first function.
The task in question:
The idea is that all the values in a matrix are given a weighted average based on given matrix W. I've already successfully implemented this code with iteration here:
W = [10/222 27/222 10/222;
27/222 74/222 27/222;
10/222 27/222 10/222];
for i=2:size(bw_im,1)-1
for j=2:size(bw_im,2)-1
A=bw_im(i-1:i+1, j-1:j+1);
bw_im(i,j)=sum(sum(A.*W));
end
end
bw_smooth=bw_im;
This successfully outputs the values the full instructions ask for. However, I've been unable to properly vectorize it. The best I've gotten is cutting out the iteration with this
bw_im(2:size(bw_im,1)-1,2:size(bw_im,2)-1)=
Which means it'll only overwrite the non-edge values. Other than that, I'm completely stumped. Please help, I'm panicking at the moment as there is still so much more to do.

Answers (1)

Just use conv2() with the valid option and then paste it onto a copy of the original. It's 3 lines of code. Hint:
conv2Image = conv2(A, w, 'valid');
result = A; % Initialize
result(2:............) = conv2Image; % Paste
I'll let you figure out the indexes in the third line to complete it. Should take you less than a minute, not 4 hours, once you realize what they are describing is convolution (actually correlation but with a symmetrical kernel correlation is the same as convolution).
If you can't use conv2() and need to do it yourself, I attach a very inefficient manual way of doing the scanning with a 4-nested for loop, though it's not recommended. I recommend using conv2 because it's highly efficient and optimized.

9 Comments

We're not allowed to use the convenient functions like conv2() or any loops. So neither of those are options.
And I'm sorry, but most of the terms you've used, convolution, correlation, kernals have all gone over my head. None of those things mean anything to me as we're entering this topic blind.
Convolution and correlation are basically scanning an array with a smaller array (called "kernel), multiplying the values of the bigger matrix underneath the smaller one by the values of the smaller, moving window and summing together. The sum goes into an output image at a location that is the center of the window. Then just slide the kernel window along until it covers the entire large matrix. Convolution has the kernel window flipped vertically and horizontally whereas correlation uses the kernel window as is (no flipping).
If you can't use the built-in function, then use my attached code where I do it "manually".
Sorry if I'm being stupid, but where exactly is the code attached? I can't see an attachment anywhere
yeah the doing it manual method still requires the use of loops, which again, is not allowed
The inner two loops can be vectorized - just use indexing to extract the 9 values in the little square window. But I don't see how you can do the whole thing without loops. The task does not say anything about vectorization.
yeah I cut out about a page of content that was referring to the project as a whole, but as I mentioned at the beginning, it has to all be vectorized
Welp I figured it out, it's the stupidest piece of code ever but it works
bw_im(2:end-1,2:end-1)=bw_im(1:end-2,1:end-2)*10/222+bw_im(1:end-2,2:end-1)*27/222+bw_im(1:end-2,3:end)*10/222+bw_im(2:end-1,1:end-2)*27/222+bw_im(2:end-1,2:end-1)*74/222+bw_im(2:end-1,3:end)*27/222+bw_im(3:end,1:end-2)*10/222+bw_im(3:end,2:end-1)*27/222+bw_im(3:end,3:end)*10/222;
There are differences between your method and the one I suggested. See attached code.

Sign in to comment.

Products

Asked:

on 24 Mar 2018

Commented:

on 24 Mar 2018

Community Treasure Hunt

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

Start Hunting!