Basic Array Syntax and Manipulations

The following exercises are meant to be answered by a single MATLAB command. The command may be involved (i.e., it may use a number of parentheses or calls to functions) but can, in essence, be solved by the execution of a single command. If the command is too complicated, feel free to break it up over two or more lines.


[Back to main page] [Back to exercises page]
1. Given x = [3 1 5 7 9 2 6], explain what the following commands "mean" by
   by summarizing the net result of the command.

  a. x(3)
  b. x(1:7)
  c. x(1:end)
  d. x(1:end-1)
  e. x(6:-2:1)
  f. x([1 6 2 1 1])
  g. sum(x)
  
2. Given the array A = [ 2 4 1 ; 6 7 2 ; 3 5 9], provide the commands needed to

  a. assign the first row of A to a vector called x1
  b. assign the last 2 rows of A to an array called y
  c. compute the sum over the columns of A
  d. compute the sum over the rows of A
  e. compute the standard error of the mean of each column of A (NB. the standard
     error of the mean is defined as the standard deviation divided by the
     square root of the number of elements used to compute the mean.)
    ans.

3. Given the arrays x = [1 4 8], y = [2 1 5] and A = [3 1 6 ; 5 2 7], determine
   which of the following statements will correctly execute and provide the result.
   If the command will not correctly execute, state why it will not. Using the
   command whos may be helpful here.
   
  a. x + y
  b. x + A
  c. x' + y
  d. A - [x' y']
  e. [x ; y']
  f. [x ; y]
  g. A - 3

4. Given the array A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5], explain the results of the
   following commands:
   
  a. A'
  b. A(:,[1 4])
  c. A([2 3],[3 1])
  d. reshape(A,2,6)
  e. A(:)
  f. flipud(A)
  g. fliplr(A)
  h. [A A(end,:)]
  i. A(1:3,:)
  j. [A ; A(1:2,:)]
  k. sum(A)
  l. sum(A')
  m. sum(A,2)
  k. [ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ] 

5. Given the array A from problem 4, above, provide the command that will

  a. assign the even-numbered columns of A to an array called B
  b. assign the odd-numbered rows to an array called C
  c. convert A into a 4-by-3 array
  d. compute the reciprocal of each element of A
  e. compute the square-root of each element of A
    ans.
  
6. Give the following commands to create an array called F:

     >> randn('seed',123456789)
     >> F = randn(5,10);
     
  a. Compute the mean of each column and assign the results to the elements of a
     vector called avg.
  b. Compute the standard deviation of each column and assign the results to the
     elements of a vector called s.
  c. Compute the vector of t-scores that test the hypothesis that the mean of each
     column is no different from zero.
  d. If Pr(|t| > 2.132 ) = 0.1 with 4 degrees of freedom, are any of the mean values
     in the vector avg statistically different from 0?
    ans.


[Back to main page] [Back to exercises page]
Comments? Contact Jim Maneval at maneval@bucknell.edu