# MatLab - Raising each component of a matrix

1. ### Aseeb

3
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. ### jhae2.718

1,160
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
3. ### Aseeb

3
Thank you, Sir.

4. ### kreil

581
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.