MatLab - Raising each component of a matrix

  1. I am in great need of the syntax that tells MatLab to raise the component of a vector to any power.

    I also need to know the syntax that outputs the time to perform the calculation.



    Thank you
     
  2. jcsd
  3. jhae2.718

    jhae2.718 1,160
    Gold Member

    If you mean raising the each component of the vector to the same power, e.g. you want ##{\boldsymbol x} ^ n = [x_0^n, x_1^n, \ldots, x_m^n]^T##, then you can use:
    Code (Text):

    [color=#408080][i]% x = some vector[/i][/color]
    x [color=#666666].^[/color] n;
     
    For timing, you can use:
    Code (Text):

    t = cputime;
    [color=#408080][i]% do some operations here[/i][/color]
    elapsed = cputime [color=#666666]-[/color] t;
    fprintf([color=#BA2121]'Elasped time: %.2f s\n'[/color], elapsed);
     
     
    Last edited: Feb 4, 2013
  4. Thank you, Sir.
     
  5. For timing, you can also use 'tic' and 'toc' as follows:

    Code (Text):
    X = 1:1000;
    tic
    X2 = X.^2;
    toc

    Code (Text):
    Elapsed time is 0.006004 seconds.
    In general, you can put any code snippet between the two tags. 'tic' always marks the beginning of the timer, and 'toc' the end.
     
Know someone interested in this topic? Share a link to this question via email, Google+, Twitter, or Facebook

Have something to add?