v = [1, 2, 3, 4, 5];
p = 2;
w = v.^p;
Output (ideone):
1 2 3 4 5
1 4 9 16 25
Question
I have a vector [x, y, ...]
in Octave and I would like to take the pth powers of the elements to get the new vector [x^p, y^p, ...]
. Anybody has an idea how to do this?
Solution
OTHER TIPS
If you want to apply an operation element wise to a vector/matrix, prepend the operator with a dot:
b=[1,2,3,4,5,6];
b2=b.^2;