2

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?

4

2 回答 2

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

输出(ideone):

1    2    3    4    5
1    4    9   16   25
于 2014-04-10T09:34:41.093 回答
3

如果您想将操作元素明智地应用于向量/矩阵,请在运算符前面加上一个点:

b=[1,2,3,4,5,6];
b2=b.^2;
于 2014-04-10T09:34:45.893 回答