1

行列(例:named )から新しい(n*m) x 4行列(例:named )を作成する必要がありますが、速度の理由からネストされたループを使用したくありません。ネストされたループでそれを行う方法は次のとおりです。bn x ma

for j in xrange(1,m+1):
    for i in xrange(1,n+1):
        index = (j-1)*n+i
        b[index,1] = a[i,j]
        b[index,2] = index
        b[index,3] = s1*i+s2*j+s3
        b[index,4] = s4*i+s5*j+s6

したがって、問題は、元の行列インデックスから派生した値を使用して新しい行列を作成する方法です。ありがとう

4

2 に答える 2

3

numpyを使用できる場合は、

import numpy as np
# Create an empty array
b = np.empty((np.multiply(*a.shape), 4), dtype=a.dtype)
# Get two (nxm) of indices
(irows, icols) = np.indices(a)
# Fill the b array
b[...,0] = a.flat
b[...,1] = np.arange(a.size)
b[...,2] = (s1*irows + s2*icols + s3).flat
b[...,3] = (s4*irows + s5*icols + s6).flat
于 2012-08-14T10:23:35.837 に答える
0

同様の質問がある人のためのいくつかのマイナーな修正(コメントとして投稿できませんでした:/):

import numpy as np
# Create an empty array
b = np.empty((a.size, 4), dtype=a.dtype)
# Get two (nxm) of indices (starting from 1)
(irows, icols) = np.indices(a.shape) + 1
# Fill the b array
b[...,0] = a.flat
b[...,1] = np.arange(a.size) + 1 
b[...,2] = (s1*irows + s2*icols + s3).flat
b[...,3] = (s4*irows + s5*icols + s6).flat
于 2012-08-16T08:30:07.420 に答える