Using Matlab solve this problem: Function Name: powerOfTwo Inputs: 1. (double) A
ID: 668782 • Letter: U
Question
Using Matlab solve this problem:
Function Name: powerOfTwo
Inputs:
1. (double) A positive whole number
Outputs:
1. (logical) A logical value specifying if the input is a power of two
Function Description:
For all our computations, we have been using the base-10 decimal number system. However, computers (and MATLAB!) store numbers using the base-2 system, called binary. A binary number consists of multiple bits of either 0 or 1. Each bit n represents the decimal value 2n, and a binary number can be converted to decimal by adding all the powers of two corresponding to each bit that is a 1.
For example, the decimal number 214 in binary is 11010110.
11010110 (binary) = 27 + 26 + 24 + 22 + 21 = 128 + 64 + 16 + 4 + 2 = 214 (decimal)
Using logical indexing, write a MATLAB function that calculates if a decimal number is a power of two. Return a logical (true or false) value specifying whether or not the decimal number is a power of two.
Notes:
Since it has not been taught in class, you should not use iteration to solve this problem.
You may not use the log(), log2(), or dec2binvec() functions.
You will only be provided positive whole numbers. There are no negative powers of two!
Hints:
The dec2bin() function will be useful. Pay attention to the output!
Explanation / Answer
C = A.^B
C = power(A,B)
Description
example
C = A.^B raises each element of A to the corresponding power in B.
C = power(A,B) is an alternate way to execute A.^B, but is rarely used. It enables operator overloading for classes.
Examples
collapse all
Square Each Element of Vector
Create a vector, A, and square each element.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.