Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function, contains(x,y), which determines if x appears anywhere within y

ID: 3644627 • Letter: W

Question

Write a function, contains(x,y), which determines if x appears anywhere within y. If they are of the same size, then x and y must be equivalent. Otherwise, x may start at any place in y and contains(x,y) will be true if all of x is equal to a patch of y. Return 1 for true, 0 for false.
In the following example, x appears in the lower right corner of y: >> x = [ 1 2;
3 4];
>> y = [ 9 8 7 6
7 6 5 4;
5 3 1 2;
3 1 3 4 ];
>> contains(x,y) ans = 1

Again I understand the core concepts, just believe I am over thinking this.

Explanation / Answer

Please rate...

Save it in a file named as "contains.m"

y is the larger matrix

x is the smaller or equivalent matrix

Matlab Program:

========================================================

function [val]=contains(x,y)
tolerance=.0001;
[m,n]=size(x);

[I,J]=find(abs(imfilter(y,x)-norm(x(:))^2)<=tolerance);

I=bsxfun(@plus,I,0:m-1);       %I(:,k) are i-coordinates of k-th submatrix
J=bsxfun(@plus,J,0:n-1);       %J(:,k) are j-coordinates of k-th submatrix
if((isempty(I)==1) && (isempty(J)==1))
     val=0;
else val=1;
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote