문제

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