sonettguy
Posts: 99
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

placing 2-dim arrays into 3-dim sheets

Wed Apr 17, 2019 2:22 pm

I need some help. I'm on a pi zero w with raspbian stretch and using python 2.7 and numpy. I'm building 2-dim arrays in a loop and would like to store each resulting array. So, I'd like to build a 3-dim array with each sheet being a loop iteration. I'm confused as to how to create the 3-D array and add elements as 2-D arrays. I refer to the manual, which states:
Indexing Multi-dimensional arrays
Things become more complex when multidimensional arrays are indexed, particularly with multidimensional index arrays. These tend to be more unusual uses, but they are permitted, and they are useful for some problems. We’ll start with the simplest multidimensional case (using the array y from the previous examples):
>>> y=np.arange(35).reshape(5,7) #I added this line from previous examples.
>>>
>>> y[np.array([0,2,4]), np.array([0,1,2])]
array([ 0, 15, 30])
In this case, if the index arrays have a matching shape, and there is an index array for each dimension of the array being indexed, the resultant array has the same shape as the index arrays, and the values correspond to the index set for each position in the index arrays. In this example, the first index value is 0 for both index arrays, and thus the first value of the resultant array is y[0,0]. The next value is y[2,1], and the last is y[4,2].
My code:

Code: Select all

for i in range(manifolds):
   for j in range(known):
      for k in range(new):
         {lots of stuff)
         print "resulting 2-D array",matmul.shape,matmul.dtype
         print matmul
         if i==0, j==0, k==0:
            correl=np.expand_dims(matmul, axis=0)
            print "correl is", correl.shape, correl.dtype
         correl(i,j,k)=correl.itemset(i, matmul[j,k])
I get

Code: Select all

   File "nameofprog.py", line 245
         correl(i,j,k)=correl.itemset(i,matmul[j,k])
SyntaxError: can't assign to function call
I don't see what is wrong.

sonettguy
Posts: 99
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: placing 2-dim arrays into 3-dim sheets

Thu Apr 18, 2019 6:56 pm

I am wondering if I might have done something to alienate the forum. This is the third topic I've posted asking for help and not getting feedback. If I did something wrong, I apologize, I certainly didn't intend to offend anyone.

pcmanbob
Posts: 5484
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: placing 2-dim arrays into 3-dim sheets

Thu Apr 18, 2019 7:10 pm

As 99.9% of people here are just other pi users it my be that no one that has looked at your post has any idea on how to solve your problem.

It can take days for the right person to see your post, 24 hours is a very short time for that to occur.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

sonettguy
Posts: 99
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: placing 2-dim arrays into 3-dim sheets

Thu Apr 18, 2019 7:55 pm

Good to know, @pcmanbob. Thanks.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11568
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: placing 2-dim arrays into 3-dim sheets

Fri Apr 19, 2019 8:56 am

Moved to python. where it might have more success.

gordon77
Posts: 3815
Joined: Sun Aug 05, 2012 3:12 pm

Re: placing 2-dim arrays into 3-dim sheets

Fri Apr 19, 2019 10:57 am

Can you provide more info, eg what are manifolds, known, new etc?

When l run it it rejects if i==0, j==0, k==0:, does this work for you?

User avatar
Paeryn
Posts: 2436
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: placing 2-dim arrays into 3-dim sheets

Fri Apr 19, 2019 2:56 pm

sonettguy wrote:
Wed Apr 17, 2019 2:22 pm

Code: Select all

         if i==0, j==0, k==0:
            correl=np.expand_dims(matmul, axis=0)
            print "correl is", correl.shape, correl.dtype
         correl(i,j,k)=correl.itemset(i, matmul[j,k])
I get

Code: Select all

   File "nameofprog.py", line 245
         correl(i,j,k)=correl.itemset(i,matmul[j,k])
SyntaxError: can't assign to function call
I don't see what is wrong.
As Python told you, you are trying to assign a value correl.itemset(i, matmul[j,k]) to a function call correl(i,j,k).
I've no experience with numpy but looking at the docs Array.itemset() returns None (it doesn't return anything), are you sure you want to assign the value None to whatever you were thinking of on the left-hand side? Which brings you on to the error that Python gave...

correl(i,j,k) says call the object correl with arguments i, j, k, but correl is an Array which is not callable and you can't assign a value to a function call, that makes no sense which is why Python gave the error there, it hadn't gotten around to trying to call correl(i,j,k) when it noticed the assignment attempt.
She who travels light — forgot something.

sonettguy
Posts: 99
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: placing 2-dim arrays into 3-dim sheets

Sun Apr 21, 2019 2:08 am

That's helpful @Paeryn. I hadn't meant the left side to be a function, but rather a specific element of a matrix. I should have used square brackets, I think. @Gordon77, I included only a section of code. Perhaps I should include the imports and variable assignments. If I still have trouble after changing the brackets, I'll do that in the next iteration of questions. In general, if you think of four dimensions as (books, sheets, rows, columns), then manifolds are books, sources are sheets. I have two 4D arrays, one is a bank of known sources and the other is a measured collection of new sources, which I compare to the known sources. None of that is really pertinent to the problem I'm having, but it might be interesting to the readers. I'll clean up my code and let you know how it goes.

Return to “Python”