如何迭代矩阵的列? [英] How to iterate over columns of a matrix?

查看:61
本文介绍了如何迭代矩阵的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,如果有定义:

In python if a define:

a = arange(9).reshape(3,3)

作为3x3矩阵并进行迭代:

as a 3x3 matrix and iterate:

for i in a:

它将遍历矩阵的行.有什么办法可以遍历列吗?

It'll iterate over the matrix's rows. Is there any way to iterate over columns?

推荐答案

如何

for i in a.transpose():

或更短:

for i in a.T:

这看起来很昂贵,但实际上却很便宜(它返回对相同数据的视图,但是其shape和stride属性已置换).

This may look expensive but is in fact very cheap (it returns a view onto the same data, but with the shape and stride attributes permuted).

这篇关于如何迭代矩阵的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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