MATLAB MatLab - Raising each component of a matrix

AI Thread Summary
To raise each component of a vector in MATLAB to a specific power, use the syntax x .^ n, where x is the vector and n is the power. For measuring the time taken to perform calculations, you can utilize the cputime function by recording the time before and after the operation, calculating the elapsed time with elapsed = cputime - t. Alternatively, the 'tic' and 'toc' functions can be employed, where 'tic' starts the timer and 'toc' stops it, providing a straightforward way to measure execution time. An example shows that squaring a vector from 1 to 1000 takes approximately 0.006 seconds.
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:
Thank you, Sir.
 
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.
 

Similar threads

Back
Top