簡體   English   中英

創建交替的零和隨機數矩陣?

[英]Create a matrix of alternating zeroes and random numbers?

我正在嘗試編寫一些代碼來創建一個交替的1 / -1和0的矩陣,即:

[-1  0 -1  0  1  0  1  0 -1  0]
[ 0  1  0 -1  0  1  0 -1  0  1]
[ 1  0  1  0 -1  0 -1  0 -1  0]
[ 0  1  0 -1  0 -1  0 -1  0  1]
[ 1  0  1  0  1  0  1  0  1  0]

我創建了一個生成零矩陣的類,並用1或-1附加它,我已經嘗試弄亂我的for循環和切片我的矩陣但我似乎無法生成我喜歡的矩陣。 我有一個中級的python知識,所以我很欣賞使用我創建的代碼解決我的問題可能不是特別優雅,但任何幫助將不勝感激。

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import random


#constants
N = 10 #dimensions of matrix


class initial_lattice: 
    def __init__(self,N):   #create initial matrix of size NxN
        self.N=N
        self.matrix_lattice()

    def matrix_lattice(self):
        self.lattice = np.zeros((N,N), dtype=int) #creates initial matrix of zeroes
        for x in range(0,N):    
            for y in range(0,N):
                self.lattice[x,y]=random.choice([1,-1]) #randomly chooses values of 1 and -1 and appends matrix

lattice1=initial_lattice(N) 


print lattice1.lattice

偶數/奇數行的想法似乎很好,一個變化:

def matrix_lattice(self):
    self.lattice = np.random.choice([-1, 1], (N, N))
    self.lattice[::2, ::2] = 0
    self.lattice[1::2, 1::2] = 0

可能有更好的解決方案,但這個肯定有效:

def matrix_lattice(m,n):
  mask = np.ones((m,n), dtype=int) # All 1s
  mask[1::2, ::2] = 0 # Clean even fields in odd rows
  mask[::2, 1::2] = 0 # Clean odd fields in even rows
  u = np.random.randint(2, size=(m,n)) * 2 - 1 # 1s and -1s      
  return u * mask # Superimpose the matrices

print(matrix_lattice(5,5))
#array([[-1,  0,  1,  0,  1],
#       [ 0, -1,  0,  1,  0],
#       [ 1,  0,  1,  0, -1],
#       [ 0,  1,  0, -1,  0],
#       [ 1,  0, -1,  0, -1]])

我更喜歡根據索引的mod制作一個過濾器,乘以一個隨機+ -1矩陣。 例如:

def my_matrix(N=10, toggle=0):

    m       = np.random.choice([-1, 1], (N, N))
    [j,k]   = np.indices([N,N])
    filter  = (j + k + toggle)%2

    return filter*m

填充隨機和歸零的Intead,我用零填充並輸入隨機值

def matrix_lattice(self):
    self.lattice = np.zeros((N, N))
    self.lattice[::2,   ::2] = np.random.choice([-1, 1], (-(-N // 2), -(-N // 2)))
    self.lattice[1::2, 1::2] = np.random.choice([-1, 1], (N // 2,     N // 2)))
#!/usr/bin/env python

import numpy as np 
import random

class SpecialMat:
    def __init__(self, m, n):
        self.dim = (m, n)  # matrix dimension
        self.members = (-1, 1)
        self.data = self.__matrix_lattice(self)

    def display(self):
        print(self.data)

    @staticmethod
    def __matrix_lattice(self):
        m = self.dim[0]
        n = self.dim[1]
        L = self.members
        a = np.zeros((m, n), dtype=int)
        for i in range(m):
            for j in range(n):
                if self._is_odd(j + i):
                    a[i, j] = random.choice(L)
        return a

    def _is_odd(self, num):
        return num % 2 is not 0

然后在Python(3.6)中:

from <yourPythonFileName> import SpecialMat
M = SpecialMat(10, 5)  # should work with any integers
M.display()
[[ 0  1  0  1  0  1  0 -1  0 -1]
 [-1  0  1  0 -1  0  1  0  1  0]
 [ 0 -1  0  1  0 -1  0 -1  0  1]
 [-1  0  1  0 -1  0  1  0  1  0]
 [ 0  1  0  1  0  1  0 -1  0 -1]]

def _is_odd來自這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM