如何仅使用一个循环使用python迭代2D矩阵的所有元素 [英] How to iterate over all elements of a 2D matrix using only one loop using python

查看:49
本文介绍了如何仅使用一个循环使用python迭代2D矩阵的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用两个索引在2d矩阵上进行迭代:

I know you can iterate over a 2d matrix using two indexes like this:

import numpy as np

A = np.zeros((10,10))

for i in range(0,10):
    for j in range(0,10):
        if (i==j):
            A[i,j] = 4

            

有没有办法仅使用一个for循环或使用切片?

Is there a way of doing this using only one for loop or using slices?

我还需要考虑i =/j的时间,例如:

I also need to take into account of when i =/ j, for example:

A = np.zeros((10,10))

for i in range(0,10):
    for j in range(0,10):
        if (i==j):
            A[i,j] = 1
        if (i+1 ==j):
            A[i,j] = 2
        if (i-1==j):
            A[i,j] = 3

推荐答案

In [129]: A = np.zeros((10,10), int)
     ...: for i in range(0,10):
     ...:     for j in range(0,10):
     ...:         if (i==j):
     ...:             A[i,j] = 1
     ...:         if (i+1 ==j):
     ...:             A[i,j] = 2
     ...:         if (i-1==j):
     ...:             A[i,j] = 3
     ...: 

您应该已经显示了生成的 A :

You should have shown the resulting A:

In [130]: A
Out[130]: 
array([[1, 2, 0, 0, 0, 0, 0, 0, 0, 0],
       [3, 1, 2, 0, 0, 0, 0, 0, 0, 0],
       [0, 3, 1, 2, 0, 0, 0, 0, 0, 0],
       [0, 0, 3, 1, 2, 0, 0, 0, 0, 0],
       [0, 0, 0, 3, 1, 2, 0, 0, 0, 0],
       [0, 0, 0, 0, 3, 1, 2, 0, 0, 0],
       [0, 0, 0, 0, 0, 3, 1, 2, 0, 0],
       [0, 0, 0, 0, 0, 0, 3, 1, 2, 0],
       [0, 0, 0, 0, 0, 0, 0, 3, 1, 2],
       [0, 0, 0, 0, 0, 0, 0, 0, 3, 1]])

因此,您设置了3个对角线:

So you have set 3 diagonals:

In [131]: A[np.arange(10),np.arange(10)]
Out[131]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
In [132]: A[np.arange(9),np.arange(1,10)]
Out[132]: array([2, 2, 2, 2, 2, 2, 2, 2, 2])
In [133]: A[np.arange(1,10),np.arange(9)]
Out[133]: array([3, 3, 3, 3, 3, 3, 3, 3, 3])

消除 numpy 中的循环的关键是获得任务的概况,而不是着眼于迭代步骤.

The key to eliminating loops in numpy is to get a big picture of the task, rather than focusing on the iterative steps.

有许多用于制作对角线阵列的工具.一个是 np.diag ,可以这样使用:

There are various tools for making a diagonal array. One is np.diag, which can be used thus:

In [139]: np.diag(np.ones(10,int),0)+
          np.diag(np.ones(9,int)*2,1)+
          np.diag(np.ones(9,int)*3,-1)
Out[139]: 
array([[1, 2, 0, 0, 0, 0, 0, 0, 0, 0],
       [3, 1, 2, 0, 0, 0, 0, 0, 0, 0],
       [0, 3, 1, 2, 0, 0, 0, 0, 0, 0],
       [0, 0, 3, 1, 2, 0, 0, 0, 0, 0],
       [0, 0, 0, 3, 1, 2, 0, 0, 0, 0],
       [0, 0, 0, 0, 3, 1, 2, 0, 0, 0],
       [0, 0, 0, 0, 0, 3, 1, 2, 0, 0],
       [0, 0, 0, 0, 0, 0, 3, 1, 2, 0],
       [0, 0, 0, 0, 0, 0, 0, 3, 1, 2],
       [0, 0, 0, 0, 0, 0, 0, 0, 3, 1]])

或改编[131]等

In [140]: A = np.zeros((10,10), int)
     ...: A[np.arange(10),np.arange(10)]=1
     ...: A[np.arange(9),np.arange(1,10)]=2
     ...: A[np.arange(1,10),np.arange(9)]=3

这篇关于如何仅使用一个循环使用python迭代2D矩阵的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆