سؤال

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