Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MatLab - Raising each component of a matrix

  1. Feb 4, 2013 #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. Feb 4, 2013 #2


    User Avatar
    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. Feb 4, 2013 #3
    Thank you, Sir.
  5. Feb 5, 2013 #4


    User Avatar
    Gold Member

    For timing, you can also use 'tic' and 'toc' as follows:

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

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook