将float传递给嵌套的for循环并存储输出

将float传递给嵌套的for循环并存储输出

本文介绍了将float传递给嵌套的for循环并存储输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab方面有很强的背景,并且我正尝试切换到python.我试图用numpy数组编写一个嵌套的for循环并存储输出值.

I have a strong background in Matlab, and I am trying to switch to python. I am trying to write a nested for loop with numpy array and storing output values.

我的代码如下:

import numpy as np
import math

# T parameter
kk = np.arange(0, 20, 0.1)
print(len(kk))

# V parameter
pp = np.arange(1, 5, 1)
print(len(pp))

a = len(kk)
b = len(pp)

P = np.zeros((a,b))


for T in kk:
    print(T)
    for V in pp:
        print(V)
        P = math.exp(-T*V/10)

print(P)

说明/问题

    向量是
  1. kk pp .在for循环中,将调用T和V参数的正确值.但是,不会存储 P 的值.

  1. kk, pp are the vectors. In for loop(s) correct values of T and V parameters are being called. However, values of P are not being stored.

我尝试了以下更改 P [T] [V] = math.exp(-T * V/10),出现以下错误: IndexError:仅整数,切片(:),省略号( ... ),numpy.newaxis( None )和整数或布尔数组是有效索引

I tried the following change P[T][V] = math.exp(-T*V/10), I get the following error: IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

任何帮助将不胜感激.预先谢谢你.

Any help will be appreciated. Thank you in advance.

推荐答案

在此代码中,您将 P 定义为2d数组.但是,循环将 math.exp 表达式的标量结果分配给该变量.这将替换原始的 P 值,并且还将替换在上一个循环中计算出的值.这种循环在MATLAB中不起作用吗?您是否不必将标量值分配给 P 中的某个插槽"?

In this code you define P as a 2d array. But the loop you assign the scalar result of the math.exp expression to that variable. That replaces the original P value, and also replaces the value calculated in the previous loop. This kind of loop doesn't work in MATLAB does it? Don't you have to assign the scalar value to some 'slot' in P?

P = np.zeros((a,b))
for T in kk:
    print(T)
    for V in pp:
        print(V)
        P = math.exp(-T*V/10)

更好的方法:

In [301]: kk = np.arange(0,20,0.1)
In [302]: kk.shape
Out[302]: (200,)
In [303]: pp = np.arange(1, 5,1)
In [304]: pp.shape
Out[304]: (4,)

numpy 中,我们更喜欢使用快速的全数组方法.在这里,我使用 broadcasting 来执行 outer ,例如使用 pp 计算 kk .

In numpy we prefer to use fast whole-array methods. Here I use broadcasting to perform an outer like calculation of kk with pp.

In [305]: P = np.exp(-kk[:,None]*pp/10)
In [306]: P.shape
Out[306]: (200, 4)

(我相信MATLAB近年来已添加了 broadcasting numpy 从一开始就拥有它.)

(I believe MATLAB added broadcasting in recent years; numpy has had it from the beginning.)

将此与迭代版本进行比较:

Comparing this with the iterative version:

In [309]: P1 = np.zeros((200,4))
     ...: for i in range(0,len(kk)):
     ...:     for j in range(0,len(pp)):
     ...:         T = kk[i]
     ...:         V = pp[j]
     ...:         P1[i,j] = math.exp(-T*V/10)
     ...:
In [310]: P1.shape
Out[310]: (200, 4)
In [311]: np.allclose(P,P1)
Out[311]: True

在Python中编写索引迭代的更简洁方法是使用 enumerate :

A cleaner way of writing indexed iteration in Python is with enumerate:

In [312]: P1 = np.zeros((200,4))
     ...: for i,T in enumerate(kk):
     ...:     for j,V in enumerate(pp):
     ...:         P1[i,j] = math.exp(-T*V/10)

这篇关于将float传递给嵌套的for循环并存储输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:26