0

I'm new to Python and I would like to write a function that, given n inputs, returns m outputs. I can do this in Matlab, with a function like:

[a,b,c,d] = my_function(x,y,z,h)

where (x,y,z,h) are multidimensional arrays.

In particular, having:

A=np.zeros((3,150,150))
B=np.zeros((3,150,150))
C=np.zeros((3,150,150))
D=np.zeros((3,150,150))
p=zero(3,1)

and given the for loop:

x.shape=(1540,1838)
step=10
jj=0
for j in range(0,1838,step):
     jj=jj+1 #tot= 184

     ii=0
     for i in range(0,1540,step):
         ii=ii+1 #total 154
         pos=x(i,j)

executing now, within the for loop, my_function, I get the output:

a=A(:,ii,jj)
b=B(:,:,ii,jj)
c=C(:,:,ii,jj)
d=D(:,ii,jj)

Ho can I write the same function in Python style?

1 Answers1

0
def my function(x, y, z, h):
  # do some compute stuff
  # ...
  return a, b, c, d
cadolphs
  • 9,014
  • 1
  • 24
  • 41
  • Is it correct write a=A(:,ii,jj) as a=A.reshape(A[:,ii,jj]) in python? I ask you 'cause i'm new in python –  Dec 07 '22 at 23:10
  • For the reshaping, that's a different question and you'd need to explain a bit more concisely what you want to achieve – cadolphs Dec 07 '22 at 23:19
  • I don't know if reshape is right for my request. How can i write a=A(:,ii,jj) in python style from Matlab style (as i wrote in my question)? –  Dec 07 '22 at 23:26
  • This should just be `a = A[:, ii, jj]` then – cadolphs Dec 07 '22 at 23:33
  • I tried, but i received: IndexError: index 150 is out of bounds for axis 1 with size 150 –  Dec 07 '22 at 23:58
  • Matlab indices start at 1, right? Because python indices start at 0... – cadolphs Dec 08 '22 at 00:10
  • Yes, but i get the same error –  Dec 08 '22 at 22:02
  • I wrote: A[:,1,2]=A[:,ii,jj] (take all the values of the second and third column and replace them progressively, with each value ii and jj of the for loop). i get: ValueError: could not broadcast input array from shape (150,) into shape (3,) –  Dec 08 '22 at 22:12