MatLab - Raising each component of a matrix

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
Aseeb
Messages
3
Reaction score
0
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
 
Physics news on Phys.org
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:
[i]% x = some vector[/i][/color]
x .^[/color] n;

For timing, you can use:
Code:
t = cputime;
[i]% do some operations here[/i][/color]
elapsed = cputime -[/color] t;
fprintf('Elasped time: %.2f s\n'[/color], elapsed);
 
Last edited:
For timing, you can also use 'tic' and 'toc' as follows:

Code:
X = 1:1000;
tic
X2 = X.^2;
toc
Code:
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.