MatLab - Raising each component of a matrix

Click For Summary

Discussion Overview

The discussion focuses on the syntax and methods for raising each component of a matrix or vector to a power in MatLab, as well as how to measure the time taken for such calculations. The scope includes technical explanations and practical coding examples.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant requests the syntax for raising each component of a vector to a power and measuring the calculation time.
  • Another participant suggests using the element-wise power operator '.^' for raising each component of the vector to the same power.
  • A different method for timing calculations is proposed, using 'tic' and 'toc', with an example demonstrating its usage.
  • Participants provide code snippets to illustrate their points regarding both raising components and timing the operations.

Areas of Agreement / Disagreement

Participants provide different methods for timing calculations, indicating that there are multiple approaches to achieve the same goal. However, there is no explicit disagreement on the syntax for raising components of a vector.

Contextual Notes

Some assumptions about the initial state of the vector or matrix are not detailed, and the discussion does not resolve which timing method is superior or preferred.

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

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K