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

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

ID: 3634565 • Letter: W

Question

Write a Matlab 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.
For example:
>> 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

Explanation / Answer

function ans = contains(x,y) clear all; rx=size(x(1,:); cx=size(x(:,1); ry=size(y(1,:); cy = size(y(:,1); if( rx>ry ||cx>cy) ans = 0; elseif ( rx==ry &&cx==cy) if(x==y) ans =1; else ans=0; end; else s = x(1,1); for i =1:ry for j =1:cy if(y(i,j)==s) if(ry-i+1>=rx && cy-j+1>=cx) xnew = y([(i:i+rx-1],[j:j+cx-1]); if(xnew==x) ans=1; break; else ans=0; break; end; else ans=0; end; end; if(ans==1) break; end; end; fprintf('ans = %d',ans) Any doubt feel free to ask but plz give lifesaver

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