Raising a vector to the power of another vector
    24 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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.
0 Commenti
Risposte (2)
  Walter Roberson
      
      
 il 21 Apr 2014
        bsxfun(@power, vectorA(:), vectorB)
3 Commenti
  Jerry Olup
 il 24 Mag 2019
				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
    
      
 il 24 Mag 2019
				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
      
      
 il 21 Apr 2014
        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 Commenti
  Walter Roberson
      
      
 il 21 Apr 2014
				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)
Vedere anche
Categorie
				Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





