SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Downloaden Sie, um offline zu lesen
Matlab and Python: Basic Operations
Programming Seminar
Wai Nwe Tun
Konkuk University
July 11, 2016
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 1 / 18
Outline
1 Programming Paradigms
2 Objected-Oriented Fundamentals
3 Basic Operations: Arrays and Lists
4 Basic Operations: Cells and Structures
5 Basic Operations: Functions
6 Basic Operations: Loops
7 References
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 2 / 18
Programming Paradigms
Imperative
a series of well-structured steps and procedures
Examples : Fortran, C, etc.
Functional
a collection of mathematical functions
Examples : Haskell, Lisp, etc.
Object-oriented
focus on code reuse
a collection of objects (instances of a class)
class as specification like blueprint or pattern
Examples: Python, Matlab, C++, etc.
Logic, Event-based, Concurrent, and others
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 3 / 18
Object-Oriented Fundamentals
Class : template for objects, with properties and methods
Object : instance of class, with definite properties and methods
Properties : data values associated with an object
Methods : functions defined in a class and associated with an object
Inheritance : class (child) derived from another class (parent)
Encapsulation and Abstraction : information hiding by using access
modifiers and introducing just some features (abstraction)
Polymorphism : method overloading, varied forms of methods having
same name with different signature
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 4 / 18
Class, Object, Inheritance Example
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 5 / 18
Example in Matlab
classdef polynomial
properties (Access=private)
coeffs = 0;
order = 0;
end
methods
function self=polynomial(arg)
self.coeffs = arg;
self.order = length(arg) -1;
end
function [] = display(poly)
str = ’␣’;
if(poly.coeffs (1) ˜=0)
str = num2str(poly.
coeffs (1));
str = strcat(str ,’+’);
end
for i=2: poly.order +1
if(poly.coeffs(i)˜=0)
...
end
...
end
end
end
end
Object Creation
function oopexamples
p1 = polynomial ([1 ,2 ,3]);
p1.display ();
p1 = polynomial ([0 ,0 ,0 ,5]);
p1.display ();
end
Output
>> oopexamples
1+2x+3xˆ2
5xˆ3
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 6 / 18
Example in Python
class Stakeholder (object):
__name = ’Ma␣Ma’
__address = ’␣’
def __init__(self , name , address):
self.__name = name
self.__address = address
def introduce(self):
print ’Hello!␣This␣is␣’ + self.
__name + ’␣from␣’ + self.
__address + ’.’
class Customer( Stakeholder ):
def introduce(self):
super(Customer , self).introduce ()
print ’I␣am␣a␣customer.’
class Supplier( Stakeholder ):
def introduce(self):
Stakeholder .introduce(self)
print ’I␣am␣a␣supplier.’
Object Creation
s1 = Supplier(’Bo␣Bo’, ’Yangon ’)
s1.introduce ()
c1 = Customer(’No␣No’, ’Yangon ’)
c1.introduce ()
Output
Hello! This is Bo Bo from Yangon.
I am a supplier.
Hello! This is No No from Yangon.
I am a customer.
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 7 / 18
Basic Operations: Arrays and Lists
Arrays : as a storage for homogeneous data types
Lists : It depends on the nature of programming language. Some
languages allow lists to contain hetrogeneous data types, e.g., in
Python.
In Python, arrays are different from lists as described above two
statements. Arrays are created from using numpy package.
In Matlab, arrays and lists are treated as the same and they can only
be used for homogeneous types. For heterogeneous ones, structure
and cell are solutions.
Array or lists can be seen as multidimensional structure such as table,
cube (matrices) or ragged array
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 8 / 18
Example in Matlab
>> a = [1 2 3 4];
>> a = 1:4
a =
1 2 3 4
>> a = [1 ’2’ ’3’ ’45’ 6]
a =
2345 % because of
heterogeneous
data types
>> length(a)
ans =
6
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 9 / 18
Example in Python
“numpy” package is used for array
creation
conversion
shape manipulation
item selection and manipulation
arithmetic, matrix multiplication, and
comparison operations
special methods such as copy, pickling,
indexing, etc.
import numpy as np
a = np.array ([0, 1, 2, 3])
a = np.arrange (4)
a = a.reshape (2 ,2)
for x in np.nditer(a):
print x,
Output
0 1 2 3
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 10 / 18
Basic Operations: Cells and Structures
For Matlab, apart from using Cell array, Structure is a solution for
heterogeneous data storage
The different between Structure and Cell is that data in Cell can be
accessed by numeric indexing while in Structure by name.
Structure can be said as class without methods but just with
properties, from end user’s point.
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 11 / 18
Example in Matlab (Cell)
function cellExample
a = cell (1 ,3);
a{1} = 4;
a{1 ,2} = ’BoBo ’;
a{3} = 0.5;
disp(a);
disp(a{1 ,3});
end
Output
cellExample
[4] ’PaPa ’ [0.5000]
function cellExample
a = cell (1 ,3);
a{1} = 4;
a{1 ,2} = ’BoBo ’;
a{3} = [1 2 3];
disp(a);
disp(a{1 ,3});
end
Output
cellExample
[4] ’BoBo ’ [1x3 double]
1 2 3
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 12 / 18
Example in Matlab (Structure)
function structureExample
student.name = ’BoBo ’;
student.mark = [50 40 43.4];
disp(student);
disp(student.mark (3));
end
Output
>> structureExample
name: ’BoBo ’
mark: [50 40 43.4000]
43.4000
function structureExample
student.name = [’BoBo ’; ’NoNo ’];
student.mark = [50;43.4];
len = size(student.name);
for i=1: len (1)
fprintf(’%s␣:␣mark␣(%f)␣nabla ’,
student.name(i ,:) , student.mark
(i,:));
end
end
Output
>> structureExample
BoBo : mark (50.00)
NoNo : mark (43.40)
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 13 / 18
Basic Operations: Function
It is to implement business logic.
It enhances code reusability and maintainability.
It has three main parts: return type, argument list, and logic
implementation.
Generally, parameter value in argument list could be changed not only
inside but also outside that function, on the condition of pass by
reference. Or for pass by value cases, the value is not changed outside
that function.
Pass by value: copy data value; Pass by reference: copy address of
data value.
Built-in data types (integer, string) are passed by value. Arrays and
objects are passed by reference.
String in some languages such as Java is passed by reference.
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 14 / 18
Example in Matlab and Python
def update1(x):
x = 3
return x
x = 2
x = update2(x)
print x
def multivalues_return ():
x = 3
y = ’hi’
return (x,y)
(a,b) = multivalues_return ()
print (‘x‘+y)
def update(x):
x.append (4)
# return x
x = [1, 2, 3]
update(x)
print x
function functionExample
function [x] = change(x)
x = strcat(x, ’hi’);
end
x = ’hello ’;
x = change(x);
disp(x);
end
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 15 / 18
Basic Operations: Loops
It is to repeatedly execute a block of code.
It can also reduce code duplicate as function does. But, loop is more
similar to recursive function, which means function calls itself.
Loop can be carried out in two ways: do loop and while loop.
With the specific number of times, do loop is used whereas in
unknown execution times or conditional dependence, while loop did.
Loop control statements (break, continue, pass(just in Python)) can
be used. Break is to terminate the loop statement and transfers
execution to the statement immediately following the loop. Continue
is to cause the loop to skip the loop remaining part but to retest its
condition. Pass is used by a developer just to remind herself/himself
that statement position needs some kind of update.
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 16 / 18
Example
def is_prime_number (num):
i = 2
isPrime = False
if num >i:
while num%i != 0:
i = i+1
if num == i:
isPrime = True
return isPrime
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 17 / 18
References
Matlab - Object-oriented Programming by Paul Schrimpf, January 14,
2009.
Lecture 5 Advanced Matlab: OOP by Mathew J. Zahr, April 17, 2014.
http://docs.scipy.org/doc/numpy/reference/index.html
http://kr.mathworks.com/
http://www.tutorialspoint.com/python/
Programming Languages: Principles and Paradigms by Allen Tucker,
Robert Noonan.
Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 18 / 18

Weitere ähnliche Inhalte

Was ist angesagt?

Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fuclimatewarrior
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basicsLuigi De Russis
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Pedro Rodrigues
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlowBayu Aldi Yansyah
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionNandan Sawant
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...Matt Harrison
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Matt Harrison
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for KidsAimee Maree Forsstrom
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 
An Intro to Python in 30 minutes
An Intro to Python in 30 minutesAn Intro to Python in 30 minutes
An Intro to Python in 30 minutesSumit Raj
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2Zaar Hai
 
java 8 Hands on Workshop
java 8 Hands on Workshopjava 8 Hands on Workshop
java 8 Hands on WorkshopJeanne Boyarsky
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced pythonCharles-Axel Dein
 

Was ist angesagt? (20)

Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlow
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
 
python codes
python codespython codes
python codes
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
 
Python
PythonPython
Python
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
An Intro to Python in 30 minutes
An Intro to Python in 30 minutesAn Intro to Python in 30 minutes
An Intro to Python in 30 minutes
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2
 
java 8 Hands on Workshop
java 8 Hands on Workshopjava 8 Hands on Workshop
java 8 Hands on Workshop
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced python
 

Ähnlich wie Matlab and Python: Basic Operations

Object Oriented Programming in Matlab
Object Oriented Programming in Matlab Object Oriented Programming in Matlab
Object Oriented Programming in Matlab AlbanLevy
 
These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2sadhana312471
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts Pavan Babu .G
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & AnswersRatnala Charan kumar
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxcargillfilberto
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxdrandy1
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxmonicafrancis71118
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answerskavinilavuG
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answersRojaPriya
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. yazad dumasia
 
Headache from using mathematical software
Headache from using mathematical softwareHeadache from using mathematical software
Headache from using mathematical softwarePVS-Studio
 
Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62Max Kleiner
 
Hierarchical free monads and software design in fp
Hierarchical free monads and software design in fpHierarchical free monads and software design in fp
Hierarchical free monads and software design in fpAlexander Granin
 
Python Training Tutorial for Frreshers
Python Training Tutorial for FrreshersPython Training Tutorial for Frreshers
Python Training Tutorial for Frreshersrajkamaltibacademy
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...South Tyrol Free Software Conference
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experiencedzynofustechnology
 

Ähnlich wie Matlab and Python: Basic Operations (20)

Object Oriented Programming in Matlab
Object Oriented Programming in Matlab Object Oriented Programming in Matlab
Object Oriented Programming in Matlab
 
Chap05
Chap05Chap05
Chap05
 
Introduction to java programming part 2
Introduction to java programming  part 2Introduction to java programming  part 2
Introduction to java programming part 2
 
These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Slides
SlidesSlides
Slides
 
Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
 
Headache from using mathematical software
Headache from using mathematical softwareHeadache from using mathematical software
Headache from using mathematical software
 
Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62
 
Hierarchical free monads and software design in fp
Hierarchical free monads and software design in fpHierarchical free monads and software design in fp
Hierarchical free monads and software design in fp
 
Python Training Tutorial for Frreshers
Python Training Tutorial for FrreshersPython Training Tutorial for Frreshers
Python Training Tutorial for Frreshers
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experienced
 

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Kürzlich hochgeladen (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Matlab and Python: Basic Operations

  • 1. Matlab and Python: Basic Operations Programming Seminar Wai Nwe Tun Konkuk University July 11, 2016 Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 1 / 18
  • 2. Outline 1 Programming Paradigms 2 Objected-Oriented Fundamentals 3 Basic Operations: Arrays and Lists 4 Basic Operations: Cells and Structures 5 Basic Operations: Functions 6 Basic Operations: Loops 7 References Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 2 / 18
  • 3. Programming Paradigms Imperative a series of well-structured steps and procedures Examples : Fortran, C, etc. Functional a collection of mathematical functions Examples : Haskell, Lisp, etc. Object-oriented focus on code reuse a collection of objects (instances of a class) class as specification like blueprint or pattern Examples: Python, Matlab, C++, etc. Logic, Event-based, Concurrent, and others Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 3 / 18
  • 4. Object-Oriented Fundamentals Class : template for objects, with properties and methods Object : instance of class, with definite properties and methods Properties : data values associated with an object Methods : functions defined in a class and associated with an object Inheritance : class (child) derived from another class (parent) Encapsulation and Abstraction : information hiding by using access modifiers and introducing just some features (abstraction) Polymorphism : method overloading, varied forms of methods having same name with different signature Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 4 / 18
  • 5. Class, Object, Inheritance Example Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 5 / 18
  • 6. Example in Matlab classdef polynomial properties (Access=private) coeffs = 0; order = 0; end methods function self=polynomial(arg) self.coeffs = arg; self.order = length(arg) -1; end function [] = display(poly) str = ’␣’; if(poly.coeffs (1) ˜=0) str = num2str(poly. coeffs (1)); str = strcat(str ,’+’); end for i=2: poly.order +1 if(poly.coeffs(i)˜=0) ... end ... end end end end Object Creation function oopexamples p1 = polynomial ([1 ,2 ,3]); p1.display (); p1 = polynomial ([0 ,0 ,0 ,5]); p1.display (); end Output >> oopexamples 1+2x+3xˆ2 5xˆ3 Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 6 / 18
  • 7. Example in Python class Stakeholder (object): __name = ’Ma␣Ma’ __address = ’␣’ def __init__(self , name , address): self.__name = name self.__address = address def introduce(self): print ’Hello!␣This␣is␣’ + self. __name + ’␣from␣’ + self. __address + ’.’ class Customer( Stakeholder ): def introduce(self): super(Customer , self).introduce () print ’I␣am␣a␣customer.’ class Supplier( Stakeholder ): def introduce(self): Stakeholder .introduce(self) print ’I␣am␣a␣supplier.’ Object Creation s1 = Supplier(’Bo␣Bo’, ’Yangon ’) s1.introduce () c1 = Customer(’No␣No’, ’Yangon ’) c1.introduce () Output Hello! This is Bo Bo from Yangon. I am a supplier. Hello! This is No No from Yangon. I am a customer. Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 7 / 18
  • 8. Basic Operations: Arrays and Lists Arrays : as a storage for homogeneous data types Lists : It depends on the nature of programming language. Some languages allow lists to contain hetrogeneous data types, e.g., in Python. In Python, arrays are different from lists as described above two statements. Arrays are created from using numpy package. In Matlab, arrays and lists are treated as the same and they can only be used for homogeneous types. For heterogeneous ones, structure and cell are solutions. Array or lists can be seen as multidimensional structure such as table, cube (matrices) or ragged array Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 8 / 18
  • 9. Example in Matlab >> a = [1 2 3 4]; >> a = 1:4 a = 1 2 3 4 >> a = [1 ’2’ ’3’ ’45’ 6] a = 2345 % because of heterogeneous data types >> length(a) ans = 6 Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 9 / 18
  • 10. Example in Python “numpy” package is used for array creation conversion shape manipulation item selection and manipulation arithmetic, matrix multiplication, and comparison operations special methods such as copy, pickling, indexing, etc. import numpy as np a = np.array ([0, 1, 2, 3]) a = np.arrange (4) a = a.reshape (2 ,2) for x in np.nditer(a): print x, Output 0 1 2 3 Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 10 / 18
  • 11. Basic Operations: Cells and Structures For Matlab, apart from using Cell array, Structure is a solution for heterogeneous data storage The different between Structure and Cell is that data in Cell can be accessed by numeric indexing while in Structure by name. Structure can be said as class without methods but just with properties, from end user’s point. Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 11 / 18
  • 12. Example in Matlab (Cell) function cellExample a = cell (1 ,3); a{1} = 4; a{1 ,2} = ’BoBo ’; a{3} = 0.5; disp(a); disp(a{1 ,3}); end Output cellExample [4] ’PaPa ’ [0.5000] function cellExample a = cell (1 ,3); a{1} = 4; a{1 ,2} = ’BoBo ’; a{3} = [1 2 3]; disp(a); disp(a{1 ,3}); end Output cellExample [4] ’BoBo ’ [1x3 double] 1 2 3 Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 12 / 18
  • 13. Example in Matlab (Structure) function structureExample student.name = ’BoBo ’; student.mark = [50 40 43.4]; disp(student); disp(student.mark (3)); end Output >> structureExample name: ’BoBo ’ mark: [50 40 43.4000] 43.4000 function structureExample student.name = [’BoBo ’; ’NoNo ’]; student.mark = [50;43.4]; len = size(student.name); for i=1: len (1) fprintf(’%s␣:␣mark␣(%f)␣nabla ’, student.name(i ,:) , student.mark (i,:)); end end Output >> structureExample BoBo : mark (50.00) NoNo : mark (43.40) Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 13 / 18
  • 14. Basic Operations: Function It is to implement business logic. It enhances code reusability and maintainability. It has three main parts: return type, argument list, and logic implementation. Generally, parameter value in argument list could be changed not only inside but also outside that function, on the condition of pass by reference. Or for pass by value cases, the value is not changed outside that function. Pass by value: copy data value; Pass by reference: copy address of data value. Built-in data types (integer, string) are passed by value. Arrays and objects are passed by reference. String in some languages such as Java is passed by reference. Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 14 / 18
  • 15. Example in Matlab and Python def update1(x): x = 3 return x x = 2 x = update2(x) print x def multivalues_return (): x = 3 y = ’hi’ return (x,y) (a,b) = multivalues_return () print (‘x‘+y) def update(x): x.append (4) # return x x = [1, 2, 3] update(x) print x function functionExample function [x] = change(x) x = strcat(x, ’hi’); end x = ’hello ’; x = change(x); disp(x); end Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 15 / 18
  • 16. Basic Operations: Loops It is to repeatedly execute a block of code. It can also reduce code duplicate as function does. But, loop is more similar to recursive function, which means function calls itself. Loop can be carried out in two ways: do loop and while loop. With the specific number of times, do loop is used whereas in unknown execution times or conditional dependence, while loop did. Loop control statements (break, continue, pass(just in Python)) can be used. Break is to terminate the loop statement and transfers execution to the statement immediately following the loop. Continue is to cause the loop to skip the loop remaining part but to retest its condition. Pass is used by a developer just to remind herself/himself that statement position needs some kind of update. Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 16 / 18
  • 17. Example def is_prime_number (num): i = 2 isPrime = False if num >i: while num%i != 0: i = i+1 if num == i: isPrime = True return isPrime Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 17 / 18
  • 18. References Matlab - Object-oriented Programming by Paul Schrimpf, January 14, 2009. Lecture 5 Advanced Matlab: OOP by Mathew J. Zahr, April 17, 2014. http://docs.scipy.org/doc/numpy/reference/index.html http://kr.mathworks.com/ http://www.tutorialspoint.com/python/ Programming Languages: Principles and Paradigms by Allen Tucker, Robert Noonan. Wai Nwe Tun (Konkuk University) Matlab and Python: Basic Operations July 11, 2016 18 / 18