Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I need to write code that accomplishes the following:

Write a Python code that produces a variable op_table that is a numpy array with three axes, i, j, and k. Define three arrays:

xi ranges from 0 (included) to 9 (included) in steps of 1,

yj ranges form 10 (included) to 11 (included) in 20 equal-size steps,

zk ranges form 10 to 106 in five steps (i.e. with six entries total), where zk=10zk−1.

Then create the final array op_table that satisfies:

op_table[i,j,k]=sin(xi)⋅yj+zk

My question lies in how to initially set the values. I've only seen numpy arrays created in manners such as np.array([1,2,3,4]) or np.arrange(10). Also, how is this set-up? Is the first column the x-axis, second the y-axis and so forth?

    import numpy as np
    import math

    xi = np.linspace(0,9, num=10)
    yj = np.linspace(10,11,20, endpoint=True)
    zk = [10, 10**2, 10**3, 10**4, 10**5, 10**6]


   op_table = np.random.rand(10,20,6)
   for i in range (0,10):
      for j in range (0,20):
         for k in range (0,6):
             op_table[i,j,k] = math.sin(xi[i]) * yj[j] + zk[k]
share|improve this question

1 Answer 1

Don't personally believe in spoon-feeding answers, but it looks like you've misinterpreted the problem. The problem doesn't actually require that you generate any matrix, except by solving the second equation. Numpy happens to have a very helpful function called linspace that does almost exactly this.

import numpy as np

xi = np.linspace(0, 10)
yj = np.linspace(10, 11, 20)

Other than that, this seems to be a math problem, and this should get you 80% of the way to a solution. If you need help with the math, there's another stackexchange for that.

More np.linspace docs: http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html Math stackexchange: http://math.stackexchange.com/

share|improve this answer
    
Wasn't looking for the answer - just a hint in the right direction on how to assign to the arrays. Thanks for the help! –  user3424684 Sep 3 at 1:13
    
So I implemented what I think works, but it says the array has the wrong shape (10,20,6), but wouldn't that be correct based off what is given? Here is what I did (edited above) –  user3424684 Sep 3 at 2:10
    
@user3424684 It's hard for me to say. The problem as detailed above is a little non-sensical in terms of pure math (zk specifically is definitely not correct as written). You should probably also not use any for loops if you've got a numpy array (it kind of defeats the purpose.) –  Slater Tyranus Sep 3 at 2:27
    
Got it. I'll try using newaxis then. But what's wrong with zk? Don't you just start at 10 and keep multiplying by 10 at each additional term until 10^6? –  user3424684 Sep 3 at 2:50
    
Nevermind, I got it. Since it said 20 equal steps in yj, that implies 21 entries, so the shape was wrong. Thanks again for the help –  user3424684 Sep 3 at 2:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.