Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

MYANMAR AEROSPACE ENGINEERING UNIVERSITY

Department of Electrical Systems & Instrumentation

EI-4005 Computer Application

Topic – Creating Matrices Week - 2

Prepared by Dr Ye Min Hein

1
Outlines of Presentation
- Objectives
- print function
- input function
- Creating Arrays
- Exercises
- References

2
Objectives
- ability to understand the importance of array dimensions and shapes.
- ability to create arrays with specific values using functions like np.zeros, np.ones, and np.full.
- ability to generate arrays with evenly spaced elements using np.arange and np.linspace.
- ability to modify elements by assignment and slicing.
- ability to combine arrays using stacking and splitting operations.

3
print function
print - prints the specified message to the screen, or other standard output device. The message can be a
string, or any other object, the object will be converted into a string before written to the screen.
e.g.
print('Hello’)

e.g.
x=10
print('Value of x is', x)

e.g.
A=2;
print('Hello',A,sep='/')

4
input function
print - allows user input.
e.g.
x = input("Enter a num ")
print("U r number is ",x)

e.g.
x = input("Enter u r name ")
print("U r number is "+x)

5
Array and Matrix in Python
An array in Python is a collection of items stored at contiguous memory locations.
Arrays are used to store a collection of similar data items, such as a list of numbers, a list of strings, or a list of
objects.
In Python, there are several types of arrays, each with its own strengths and weaknesses.
Some of the common types are:
- Built-in Arrays, array Module Arrays, NumPy Arrays, Pandas Series and DataFrames,
Several types of matrix can be use used in Python. Some of them are
- NumPy Arrays, Pandas DataFrames, Special Matrices

6
Creating Arrays
numpy.array – create array
E.g
import numpy as np
A1=np.array([1,2,3,4])
print(A1)
E.g
import numpy as np
A1=np.array([1,2,3,4])
A2=np.array([[1,2],[3,4]])
print("1D array \n ",A1)
print((A1.ndim))
print("\n2D array \n ", A2)
7
print((A2.ndim))
Continued;
numpy.zeros - Return a new array of given shape and type, filled with zeros
numpy.ones - Return a new array of given shape and type, filled with ones.
numpy.random.randit - Return random integers from low (inclusive) to high (exclusive)
numpy.random.randit - Random values in a given shape.
numpy.empty - Return a new array of given shape and type, without initializing entries
numpy.full - Return a new array of given shape and type, filled with fill_value.
numpy.arrang - Return evenly spaced values within a given interval.
numpy.linespace - Return evenly spaced numbers over a specified interval.

8
Continued;
E.g
import numpy as np
A1=np.zeros((3,3))
A2=np.ones([3,3])
A3=np.random.randint(0,5,size=(1,10))
A4=np.random.randint(0,5,size=(3,3))
A5=np.random.randint(0,90,size=(10,1))
A6=np.random.rand(3,3)
A7=np.empty((3,4))
A8=np.full((3,4),5)
A9=np.arange(3,15,2)
A10=np.linspace(2,5,5,endpoint=True)
9
Continued;
print("\n 3 x 3 matrix with 0\n",A1)
print("\n 3 x 3 matrix with 1\n",A2)
print("\n 1 x 10 matrix (column vector) with random iteger\n ",A3)
print("\n 3 x 3 matrix with random iteger \n",A4)
print("\n 10x 1 matrix (row vector) with random iteger\n ",A5)
print("\n 3 x 3 matrix with random float between 0 to 1 \n",A6)
print("\nNumPy 2D 3 x 4 matrix with empty\n",A7)
print("\nNumPy 2D 3 x 4 matrix with 5\n",A8)
print("\nNumpy array using arrang function\n",A9)

10
Exercises
1. Create a NumPy column vector with 10 elements, filled with zeros.
2. Create a NumPy row vector with 5 elements, filled with ones.
3. Create a 2D NumPy array with 2 rows and 3 columns, filled with float between 0 to 1:
4. Create a 2D NumPy array with 3 rows and 4 columns, filled with between 10 to 20:
5. Create a NumPy row vector with 7 elements, filled with 3.
6. Create an array of 11 evenly spaced numbers between 10 and 20, excluding the endpoints.
7. Create an array of even numbers from 2 to 10 (inclusive) using NumPy.

11
References

(1) Title –MATLAB for Engineers


ISBN - 978-0-13-348597-4
(2) https://www.w3schools.com/python/numpy/default.asp

(3) https://www.w3schools.com/python/numpy/numpy_intro.asp

12

You might also like