Professional Documents
Culture Documents
1 Given a vector k of positive integers, output a vector z which contains all
prime numbers in k.
For ex : if k=[2 5 6 7 9], then z=[2 5 7]
Q.2 A perfect number is a positive integer that is equal to the sum of its proper
positive divisors, that is, the sum of its positive divisors excluding the number
itself. For ex : 6 is a perfect no. as 6=1+2+3.
Now Create a vector k of positive integers, output the vector z which contains
only perfect numbers in k.
Q.3 Create a 3 * 3 matrix A =
(i) Now create an upper triangular matrix U of A i.e. U should come as
(ii) Create a Lower triangular matrix L of A i.e. L should come as
Q.4 Trace of a square matrix is sum of its main diagonal elements. Create an 3*3
square matrix and find its trace.
Q.5 Create a 3*3 matrix A =
Find total number of digits in this matrix A.
Q.6 A square matrix is a magic square with equal row, column and diagonal sums.
Create a 3*3 square matrix A and find if it is a magic square or not. Use sum
function.
Q.7 A number is called “palindrome number” if it reads same in both directions.
For ex : 2332 and 121 are palindrome number while 1231 is not.
Create a vector vin of positive integers and output a vector vout containing only
palindrome numbers in vin.
Multiple Choice Questions
Q.8 Suppose I first execute the following Matlab commands:
A = [1 2; 3 4; 5 6];
B = [1 2 3; 4 5 6];
Which of the following are then valid Matlab commands ? (Hint: A' denotes the
transpose of A.)
• C = A * B;
• C = B * A;
• C = B + A;
• C = B' * A;
Q.9 Let A =
Which of the following indexing expressions gives B =
• B = A(1:4, 3:4);
• B = A(:, 3:4);
• B = A(:, 0:4);
• B = A(3:4, 1:2);
Q.10 Let A be a 10x10 matrix and x be a 10‐element vector. Your friend wants to
compute the product Ax and writes the following code :
v = zeros(10, 1);
for i = 1:10
for j = 1:10
v(i) = v(i) + A(i, j) * x(j);
end
end
How would you write this code to run without any for loops?
• v = Ax;
• v = sum (A * x);
• v = A * x;
• v = A .* x;
Q.11 Say you have two column vectors v and w, each with 7 elements (i.e., they
have dimensions 7x1). Consider the following code:
z = 0;
for i = 1:7
z = z + v(i) * w(i);
end
Which of the following correctly compute z?
• z = v' * w;
• z = w' * v;
• z = v .* w;
• z = w * v;
Q.12 In Matlab, many functions work on single numbers, vectors, and matrices.
For example, the sin function when applied to a matrix will return a new matrix
with the sin of each element. But you have to be careful, as certain functions have
different behavior. Suppose you have an 7x7 matrix X. You want to compute the
log of every element, the square of every element, add 1 to every element, and
divide every element by 4. You will store the results in four matrices, A,B,C,D. One
way to do so is the following code:
for i = 1:7
for j = 1:7
A(i, j) = log (X(i, j));
B(i, j) = X(i, j) ^ 2;
C(i, j) = X(i, j) + 1;
D(i, j) = X(i, j) / 4;
end
end
Which of the following correctly compute A,B,C, or D?
• D = X / 4;
• B = X ^ 2;
• C = X + 1;
• A = log (X);
Do the Following questions without using any loop :
Note : To input a number from user, use input function.
For ex : n = input(‘Enter a vector’);
Q.13 Input a positive integer k from user, find whether it is prime or not. (Don’t
use in‐built isprime function ).
Q.14 Input a positive integer p from user, find whether it is perfect number or
not.
Q.15 Create a vector v = [3, 13, 45, 56, 21, 23, 43, 44, 56, 67, 78, 89 ]. Output
vectors E and O containing only even and odd numbers of v respectively.
Q.16 Create a vector V = [3, 0, 1, 5, 0, 0, 7, 5, 0, 1, 8]. Now in V, turn all zeros to 1
and all non‐zeros to 0. Don’t use any other variable.
Q.17 Create a vector V = [3, 0, 1, 4, 0, 5, 7, 1, 1, 4, 0]. Now in V, turn all numbers
(except 1s) to zeros. Again, don’t use any other variable.
Q.18 Create a vector V=[1, 2, 3, 4, 5] of size 1*5. Now input a number n from user
and create a matrix M of size n*5 in which each row is V.
For ex : if n=3, then M =
Remember, you don’t know n in advance, so you can’t just do M=[V;V;V]
Note : There is a repmat function in Matlab which does exactly this (with some
more capabilities). Don’t use that function here.
Q.19 Create a vector V=[1, 3, 5, 7, 8] of size 1*5. Now create a matrix M of size
5*10 where each row is a table of each element of vector V.
For ex : M here would be
Q.20 Input a positive number n from user and find its number of digits.