Raising a vector to the power of another vector

36 次查看(过去 30 天)
I am totally lost and dont know where to begin. I have two vectors,
VectorA=[2.5,0,5,4.5,9.9,.123]
VectorB=[1,2,3]
and i have to take the elements in VectorA and raise them to the power in VectorB using for loops and one without..
I tried a=(vectorA.^vectorB) and that did not work.

回答(2 个)

Walter Roberson
Walter Roberson 2014-4-21
bsxfun(@power, vectorA(:), vectorB)
  3 个评论
Jerry Olup
Jerry Olup 2019-5-24
I tried this with just power(A(:),B) and it works.
Also scaled a vector using power(2,g) where g is the gain setpoint vector of a system... so implicit expansion seems built-in.
Where's the use case that forces bsxfun? It seems that implicit expansion is fine (in 2018b and perhaps later) at the least.
Good solution - leads to more exploration.
Thanks.
Steven Lord
Steven Lord 2019-5-24
Jerry Olup: the use case that forces bsxfun is the fact that Walter wrote that answer in 2014, a couple years before implicit expansion was introduced in release R2016b.

请先登录,再进行评论。


Geoff Hayes
Geoff Hayes 2014-4-21
Hi Kimberly,
First thing is that you should figure out the dimensions of your output matrix. Presumably it will be something like mxn where m is the number of elements in vectorB and n is the number of elements in vectorA. You can figure out these two values using either length or numel.
The case where you have to use for loops is simpler than the other: you will need an outer for loop to iterate over each element in vectorA, and an inner for loop to iterate over each element in vectorB. The idea here is that we will start with the first element of vectorA, raise it to the power of the first element in vectorB and assign this value to the (1,1) output matrix element. Then raise the first element of vectorA to the power of the second element of vectorB and assign it to the (2,1) output matrix element and continue for each remaining element in vectorB. We then move to the second element of vectorA and repeat as before until all elements of vectorA have been raised to all elements of vectorB.
An alternative to the above is to use something similar to what you proposed in your question and just iterate over each element in vectorB and raise vectorA to that element:
vectorA.^vectorB(i)
for i=1.....m, assigning the full row (i,:) to the output matrix.
Try this first and then see about moving on to the code without a for loop.
Geoff
  2 个评论
Kimberly
Kimberly 2014-4-21
ok this is what I have and it works but it will print only the last set of B. How do I get it to show the ones in between.
n= numel (VectorA)
m= numel (VectorB)
povec=m.*n
for i=1:m
B=VectorA.^VectorB(i)
end
fprintf('%8.2f \' ,B,)
Walter Roberson
Walter Roberson 2014-4-21
I do not see what povec is doing in your code?
In your loop, your assignment should be to
B(i,:)
instead of to B.
You are going to want to rethink your fprintf() output format. And hint: instead of passing in B to fprintf, you are going to want to pass in transpose(B)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by