Coursera: Machine Learning-Andrew NG (Week 2) Quiz - Octave / Matlab Tutorial

 These solutions are for reference only.
try to solve on your own
but if you get stuck in between than you can refer these solutions

there are different set of questions ,
we have provided the variations in particular question at the end.
read questions carefully before marking

--------------------------------------------------------------------

Octave/Matlab Tutorial

                            






explanation:
1. 
>> C = A * B;
>> C
C =

    9   12   15
   19   26   33
   29   40   51
2.
>> C = B' + A;
>> C
C =
   
       2    6
       5    9
       8   12
3.
>>  C = A' * B;
   error: operator *: nonconformant arguments (op1 is 2x3, op2 is 2x3)
4. 
>> C = B + A;
error: operator +: nonconformant arguments (op1 is 2x3, op2 is 3x2)









explanation:

A = [16 2 3 13; 5 11 10 8; 9 7 6 12; 4 14 15 1]
A =

   16    2    3   13
    5   11   10    8
    9    7    6   12
    4   14   15    1
 B = A(:, 1:2);
B =

   16    2
    5   11
    9    7
    4   14
B = A(1:4, 1:2);
B =

   16    2
    5   11
    9    7
    4   14

 B = A(:, 0:2)
error: subscript indices must be either positive integers less than 2^31 or logicals

B = A(0:4, 0:2);
error: subscript indices must be either positive integers less than 2^31 or logicals




















explanation:
 z = sum (v .* w)
z =  32


 z = w' * v
z =  32


z= v * w'
z =

    4    5    6
    8   10   12
   12   15   18


z = w * v'
z =

    4    8   12
    5   10   15
    6   12   18


z = 0;
 for i = 1:3,
     z = z + v(i) * w(i);
 end;
z =  32










explanation:


> X = [1 2 3; 4 5 6; 7 8 9]
X =

   1   2   3
   4   5   6
   7   8   9

    for i = 1:3,
> for j = 1:3,
> 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;

A =

    0.00000    0.69315    1.09861   13.00000
    1.38629    1.60944    1.79176    8.00000
    1.94591    2.07944    2.19722   12.00000
    4.00000   14.00000   15.00000    1.00000


B =

    1    4    9
   16   25   36
   49   64   81
    4   14    0

C =

    2    3    4
    5    6    7
    8    9   10


D =

   0.25000   0.50000   0.75000
   1.00000   1.25000   1.50000
   1.75000   2.00000   2.25000


 X + 1=


    2    3    4
    5    6    7
    8    9   10


 X/4=

   0.25000   0.50000   0.75000
   1.00000   1.25000   1.50000
   1.75000   2.00000   2.25000


X .^ 2
=

    1    4    9
   16   25   36
   49   64   81


B=

    1    4    9
   16   25   36
   49   64   81
    4   14    0


X ^ 2=

    30    36    42
    66    81    96
   102   126   150






--------------------------------------------------------


variation in question 5



  1. In Octave/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? Check all that apply.
    • 1) C = X + 1;
    • 2) D = X / 4;
    • 3) A = log (X);
    • 4) B = X ^ 2;


answers:1,2,3








---------------------------------------------------------------------------------

reference : coursera


darkmode