0

I have the following prior_total built from prior_fish array and np.arange(5) with num_prior_loop=50

# Declare prior array
prior_start = 1
prior_end = 5
prior_fish = np.logspace(prior_start,prior_end,num_prior_loop)
# Prior total
prior_total = np.stack([prior_fish.T, np.arange(5)])

How to get access to prior_total[i,j], I mean i for the i-th element of prior_fish and j for the j-element of np.aranage(5) ?

I tried : prior_total[i,j] and prior_total([[[0,i],[1,j]]] but this doesn't work.

What might be wrong here?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0
a = np.logspace(1,5,5)
b = np.arange(5)
c = np.stack([a.T, b])

c[i,j] returns a single element, at col i row j

c[0,i], c[1,j] returns two elements, the ith element of a and the jth element of b

Ricoter
  • 665
  • 5
  • 17
  • This doesn't work since the number of rows and columns is different, how can I do ? –  Feb 08 '21 at 11:14
  • `prior_total = np.stack([prior_fish.T, np.arange(5)])` produces an error : **raise ValueError('all input arrays must have the same shape') ValueError: all input arrays must have the same shape** –  Feb 08 '21 at 11:24
  • `num_prior_loop` is equal to `50` in my case –  Feb 08 '21 at 11:26
  • `np.arange(num_prior_loop)`, a and b should have the same length at index 0, check this link https://numpy.org/doc/stable/reference/generated/numpy.stack.html – Ricoter Feb 08 '21 at 11:29
  • thanks but I just want to concatenate 2 arrays to build a 50x5 2D array –  Feb 08 '21 at 11:33
  • That is not possible with concatenate. Do you want 5x your prior fish? or a dot product with prior fish with arange(5)? `np.dot(a[None,:], b[:,None])`, you can find more info here: https://stackoverflow.com/questions/28578302/how-to-multiply-two-vector-and-get-a-matrix – Ricoter Feb 08 '21 at 12:20
  • if you want 5x prior_fish, make `b = np.ones(5)` – Ricoter Feb 08 '21 at 12:21
  • Thanks for your help –  Feb 08 '21 at 17:18