input matrix in python using numpy code example
Example 1: how to get matrix element in the form of matrix in python
R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
matrix = []
print("Enter the entries rowwise:")
for i in range(R):
a =[]
for j in range(C):
a.append(int(input()))
matrix.append(a)
for i in range(R):
for j in range(C):
print(matrix[i][j], end = " ")
print()
Example 2: use of numpy matrix in tkinter python 3
from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageTk
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
def num():
n1 = int(t1.get())
n2 = int(t2.get())
n3 = int(t3.get()) / 100.00
top.destroy()
initBoard = np.zeros((n1, n2))
for row in range(0, n1):
for column in range(0, n2):
initBoard[row][column] = np.random.choice(np.arange(0, 2), p=[1 - n3, n3])
ax.imshow(initBoard)
canvas.draw_idle()
root = Tk()
root.title('Game of Life')
root.geometry('800x600')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis('off')
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=4, column=0)
top = Toplevel(root)
Label(top, text="How many rows?: ").grid(row=0)
Label(top, text="How many columns?: ").grid(row=1)
Label(top, text="Probability of spawn (between 0 and 100): ").grid(row=2)
t1 = Entry(top)
t2 = Entry(top)
t3 = Entry(top)
t1.grid(row=0, column=1)
t2.grid(row=1, column=1)
t3.grid(row=2, column=1)
Button(top, text='Generate', command=num).grid(row=3, column=1, sticky=W, pady=4)
mainloop()