简体   繁体   English

使用for循环从3D矩阵调用对象

[英]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. 我有一个带有x和y坐标的地图数组,每个(x,y)坐标都与一个数字有关。 I collected a bunch of these maps with the same dimensions and organized them into a 3D matrix. 我收集了一堆尺寸相同的地图,并将它们组织成3D矩阵。

Now I want to call all of the numbers in the x,y coordinates for all times one at a time. 现在,我想一次一次调用x,y坐标中的所有数字。

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 . 也就是说,每个tmaprow (第一维)。

You tried to use t as the last index. 您尝试使用t作为最后一个索引。 To do that you need to generate a range of numbers, eg [0,1,2...] 为此,您需要生成一个数字范围,例如[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 . 将在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 . arange生成它们的顺序给出值(以及将它们存储在map的顺序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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