CS代考 Multi-dimensional Arrays – cscodehelp代写

Multi-dimensional Arrays
Sue Inn Chng
The University of 1

Copyright By cscodehelp代写 加微信 cscodehelp

Revision: Arrays
– An array is a contiguous block of memory containing multiple values of the same type.
– Theelementsofanumpyarraywillalwaysbethesametype. – APythonlistmaycontainelementsofdifferentdatatypes.
– To access an element in a list or numpy array, we can use indexing. The first element is always at index-0. Example: ls[0]
– Anarraywillalwaysknowitsownlength. – Revision-Arrays
The University of 2

Revision: while Loops
– Twotypesofloops:
– Count-controlled loops (known) – Event loops (unknown)
– while loops can be used to implement both types of loops.
– for loops are used only for count-controlled loops.
– May differ for different programming languages.
The University of 3

Extra in C: while loop
The University of 4

Extra in C: for loop
The University of 5

Python: while loop vs for loop
– Spot the similarities between both code snippets:
– Canyouidentifytheseintheforloop? – Loopcontrolvariable
– Terminatingcondition
– Modificationofcontrolvariable
The University of 6

Syntax: for loop
– PythonforstatementworksdifferentlyfromC!
– Nostatementstospecify:terminatingconditionoriterationstep – Iterateoverasequence
for in : code block
1. initialise with the first item of the collection.
2. Execute everything in code block
3. Assign variable to the next element. Do Step 2.
– Go to Ed Lesson and do Part 1. The University of 7

range(start, stop, end)
– Way to construct a sequence of numbers.
– Commonly used for looping a specific number of times in for
– range(stop)
• creates seq. from 0 to integer specified by stop-1
• range(3) sequence: 0,1,2 – range(start, stop, step)
• createsseq.fromstarttointegerspecifiedbystop-1 • If step is unspecified, by default is 1
• range(1,4) sequence: 1,2,3
• range(1,10,2)sequence: 1,3,5,7,9
https://docs.python.org/3/library/stdtypes.html#typesseq-range
The University of 8

Multidimensional Array
– Real-worlddatacomesinmanyforms: – Scalar
– Vector,1D
– Matrix,2D
– N-Dimensional?
• 3D – x, y, z coordinates
The University of 9

Multi-dimensional Array – 2D
123 3 rows 4 5 6
0 [0] 0 [1] 0 [2] [1][0] [1][1] [1][2] 2[0] 2[1] 2[2]
Create matrix in Python:
>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Accessing elements of the matrix:
– Syntax:matrix[row][column]
– Example:
>>> print(m[1][2])
Try out the codes in this Ed Lesson. The University of 10

Multi-dimensional Array – 3D
– Let’slookat3-Darray:
1641 32 3262 51
5 1 [0]0 0 [0]0 1 [0]10 [0]11 [0]20 [0]21
[1]00 [1]01 [1]10 [1]11 [1]20 [1]21
– GotoEdLessonandrundemo.py. The University of 11

– forsyntax:
for in : code block
– PythonforstatementisdifferentfromCandJava.
– Count-controlledloopsonly
– Applications:Accessingelementsinvectors,matricesand multidimensional array.
– Did you revise these topics while doing the exercises? – Functions
– Conditionals – Loops
The University of 12

Extra: Numpy Arrays
– Python default behaviour for dealing with list of numbers is difficult to do complex calculations.
– NumPyallowsforefficientoperationsonarrayscomparedto list.
– Coreobjectisthendarray,amultidimensionalarrayofasingledata type
– Operations:reshape,sorted,subjecttomathematicaloperationsand statistical analysis.
– Supportsvectorization:singleoperationcanbecarriedoutonanentire array without requiring an explicit loop.
The University of 13

Extra: Num
1. ImportNumPyintoprogram. Py as np.
– import numpy as np
2. Create an array
– np.array constructor to create vectors, matrices or tensor – Filledones,zeros,empty:np.ones,np.zeros
– Sequenceofnumbers:np.linspace,np.arrange
3. ndarray attributes
Each array has its own properties.
The University of 14
shape – dimensions; size along each of its axes.
dtype – data type
ndim – number of axes(dimensions). Note ndim == len(shape)
size – total number of elements in the array, product of the elements of shape.

Reading This Week
– PythonOfficialTutorial:
– https://docs.python.org/3/tutorial/controlflow.html#for-statements
– NumpyQuickstart:
– https://numpy.org/doc/stable/user/quickstart.html
The University of 15

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

Leave a Reply

Your email address will not be published. Required fields are marked *