简体   繁体   中英

Calling Object from 3D Matrix with a for loop

I have an array of maps with x and y coordinates, and an each (x,y) coordinate relates to a number. I collected a bunch of these maps with the same dimensions and organized them into a 3D matrix.

Now I want to call all of the numbers in the x,y coordinates for all times one at a time.

I thought it would be like

    for t in Map_Collection:
        for x, y in Map_Collection[:,:,t]
            print Map_Collection[x,y] 
            #hoping it would give a large list of single numbers    

This didn't work out giving me multiple errors and I can't figure out the cause. But I think my entire logic might have been off.

To clarify I wanted to be able to find each number each coordinate of the map for each time-stamp, and use it in a separate function which I already have.

Please help.

Lets start simple:

 map=np.arange(2*3*4).reshape(2,3,4)
 for t in map:
     print t
     print

produces

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

[[12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]

That is, each t is a row (1st dimension) of map .

You tried to use t as the last index. To do that you need to generate a range of numbers, eg [0,1,2...]

for i in range(map.shape[-1]):
    for j in range(map.shape[1]):
        for k in range(map.shape[0]):
            print map[k,j,i]

Will print all the entries in map . It's not pretty, but it gives you a starting point. You may need to reorder the iterators.

Here's a way of using the 'rows' to work your way down to simple entries:

for t in map:
    for x in t:
        for y in x:
            print y

gives the values in the order that arange generated them (and the order in which they are stored in map .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM