Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day99#100DaysChallenge- Matlab Loops |Finding Perfect Squares in a vector -Matlab Cody

#Day99-Finding Perfect Squares in a vector -Matlab Cody

Task:
Given a vector of numbers, return true if one of the numbers is a square of one of the other numbers. Otherwise return false.
Example:
Input a = [2 3 4]
Output b is true
Output is true since 2^2 is 4 and both 2 and 4 appear on the list.

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab Code:

function b = isItSquared(a)

 

  for i=1:1:length(a)

      for j=1:1:length(a)

          if (a(i))^2==a(j)

              tf(i,j)=1;

          else

              tf(i,j)=0;

          end

      end

  end

  if sum(sum(tf(:,:))>0)

      b=1;

  else

      b=0;

  end

 

end

Sample Input and Output

a = [6 10 12 14 101];

assert(isequal(isItSquared(a),false))

>> a = [6 10 12 14 36 101];

assert(isequal(isItSquared(a),true))

>> a = [1,2,3,1,4,9];

>> b=isItSquared(a)


b =


     1

Click here for video description



 


No comments

Popular Posts

Followers