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?

有帮助吗?

解决方案

v = [1, 2, 3, 4, 5];
p = 2;
w = v.^p;

Output (ideone):

1    2    3    4    5
1    4    9   16   25

其他提示

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top