機械学習テストWeek 2_2_OctaveMatlab Tutorial

3802 ワード

Week 2 | 2_OctaveMatlab Tutorial


第1題


Suppose I first execute the following in Octave/Matlab: A = [1 2; 3 4; 5 6]; B = [1 2 3; 4 5 6]; Which of the following are then valid commands? Check all that apply. (Hint: A’ denotes the transpose of A.)
  • C = A’ + B;
  • C = B * A;
  • C = A + B;
  • C = B’ * A; **A B Aは3 x 2 B 2 x 3のオプション1:Aの転置は2 x 3で、Bは2 x 3で、行列は同じで加算することができます.正解2:Aは3 x 2、Bは2 x 3、乗算後は3 x 3の行列である.正しいオプション3:Aは3 x 2、Bは2 x 3で、行列が違うと加算できません.正しくない選択肢4:Bの転置は3 x 2、Aは3 x 2で乗算できない.不正**
  • 第2題


    Let A=⎡⎣⎢⎢⎢16594211714310615138121⎤⎦⎥⎥⎥ Which of the following indexing expressions gives B=⎡⎣⎢⎢⎢16594211714⎤⎦⎥⎥⎥ ? Check all that apply.
  • B = A(:, 1:2);
  • B = A(1:4, 1:2);
  • B = A(:, 0:2);
  • B = A(0:4, 0:2); ** Aを取る前の2列がB A(:,1:2)すべての列がA(1:4,1:2)の省略形**
  • 第3題


    Let A be a 10x10 matrix andx 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 vectorize this code to run without any for loops? Check all that apply.
  • v = A * x;
  • v = Ax;
  • v = x’ * A;
  • v = sum (A * x); * A 00∗x 0+A 01∗x 1+A 02∗x 2+....A*x*
  • です

    第4題


    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 vectorizations correctly compute z? Check all that apply.
  • z = sum (v .* w);
  • z = w’ * v;
  • z = v * w’;
  • z = w * v’;

  • **解答:1 vは7 x 1 wは7 x 1で、上述のコードの最終的な結果は1つの数値のオプション1:v=[12 3]w=[4 5 6]v.*w=[4 10 18]で、更に[4 10 18]に対して和を求める.正解2:wの転置が1 x 7、vが7 x 1である、v*wは数値である.正解3:vは7 x 1、w転置は1 x 7、v*wは7 x 7の行列である.不正な選択肢4:wは7 x 1、v転置は1 x 7である、v*wは7 x 7の行列である.不正**

    第5題


    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
  • C = X + 1;
  • D = X/4;
  • A = log (X);
  • B = X ^ 2; Which of the following correctly compute A,B,C, or D? Check all that apply. ** 答え:A B Cオプション4:X^2=X*Xはマトリクス乗算、ベクトル両乗の結果ですが、プログラム中の(Xi)j^2は、(Xi)jに対して単一の数値を二乗してマトリクスに書くもので、両者の意味が異なる正しい書き方はB=X.*X **