MatLab - Raising each component of a matrix

  • MATLAB
  • Thread starter Aseeb
  • Start date
  • #1
3
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
 

Answers and Replies

  • #2
jhae2.718
Gold Member
1,161
20
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:
[color=#408080][i]% x = some vector[/i][/color]
x [color=#666666].^[/color] n;
For timing, you can use:
Code:
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:
  • #3
3
0
Thank you, Sir.
 
  • #4
kreil
Insights Author
Gold Member
668
67
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.
 

Related Threads on MatLab - Raising each component of a matrix

Replies
10
Views
847
Replies
5
Views
2K
Replies
5
Views
541
  • Last Post
Replies
1
Views
5K
  • Last Post
Replies
0
Views
4K
Replies
1
Views
13K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
8
Views
5K
  • Last Post
Replies
7
Views
5K
Replies
1
Views
553
Top