3 views

Uploaded by Bishoy Emile

7

7

Attribution Non-Commercial (BY-NC)

- Homework 1 Key
- matlab.pdf
- Matlab
- Introduzione Alla Programmazione Dei SAD
- capl
- Pure Maths
- Excel Formula s(1)
- homework helper-grade 3 module 1
- Client Data Set in Detail7
- Maaa 037053
- IFEM.Ch31
- homework helper-grade 3 module 1
- Computer Applications Lecture Notes
- Designing Controller by State Space Techniques Using Reduced Order Model Algorithm
- Kruithof Double Factor More
- Utility Classes 2
- 1D FEM
- 02 GENN004 Declaration
- News
- Matrix calculator

You are on page 1of 9

09-Feb-13

Matrices

Vectors and Matrices

Outline

Vectors and Matrics Array Initialization Subarrays Loops with arrays/matrices

Matrices

09-Feb-13

Scalar vs Array

Scalar: indicates a variable that holds only a single value at a time.

This is compared to an array, which holds many values at once

Array A = (10, 20, 30, 40, 50) A(1)=10 A(2)=20 A(3)=30

A Matrix is defined as an array with two or more dimensions Vectors are defined as arrays with either one row and many columns, or one column and many rows

A row vector has many columns e.g. it is a single row A column vector has many rows e.g. it is a single column

Matrices

09-Feb-13

Array initialization

B = [1.0 2.0 3.0] creates a 1 x 3 array

Is this a column or row vector?

The semicolon ; tells MATLAB to start a new row in the array You can also separate rows by pressing enter and continuing on the next line C= [1.0 2.0 3.0]

Array initialization

D = [1, 2, 3; 6, 5, 4] creates a 2 x 3 array

The commas are optional

E = [ ] creates a special empty array having no rows and no columns F = [ 1, 5, 6; 5, 4 ] would create ...?

An error message! All rows must have the same size, or the array is not valid

Matrices

09-Feb-13

Array initialization

MATLAB includes a very useful colon operator

Makes creating sequences very simple

The syntax is

first:increment:last

first is the first value in the series increment is how big each gap between values should be increment can be skipped last is the final value in the series my_colon = 1:2:10 my_colon = [ 1 3 5 7 9 ]

The transpose operator swaps rows and columns of arrays it is applied to X = [ 1 2 3 ]; 1 Y = X; Y = 2

3

3 3 1 1

Matrices

09-Feb-13

zeros(n) zeros(m,n) zeros(size(arr)) ones(n) ones(m,n) ones(size(arr)) eye(n) eye(m,n) length(arr) n x n matrix of zeros m x n matrix of zeros matrix of zeros the size of arr n x n matrix of ones m x n matrix of ones matrix of ones the size of arr n x n identity matrix m x n identity matrix Returns length of vector or longest dimension of array Returns two values: number of rows and number of columns

size(arr)

The elements are numbered sequentially (starting from 1); each element number is called the index, or subscript. Examples: >> a=1:2:9 Index a(4)=?, a(2)=? Element a(0)=?, a(6)=? >> mat = [2:4; 3:5] mat = 234 345 mat(2,3) = ? mat(3,2) = ?

Matrices

09-Feb-13

Subarrays

MATLAB allows special subarray use array = [1.1 -2.2 3.3 -4.4 5.5];

array(3) is 3.3 array([1 4]) is itself an array, [1.1 -4.4], or the first and fourth elements in array array(1:2:5) is array [1.1 3.3 5.5] or the odd elements in array

array(end) would be 5.5 array(3:end) would be [3.3 -4.4 5.5]

Subarrays

MATLAB allows advanced subarray indexing on the left side of an assignment

array =

1 2 3 4 5 6 7 8 9 10 11 12 The shape of the left hand side subarray index

array =

22 6 7 23 9 10 11 12

Matrices

09-Feb-13

Test it out!

Try the following statements out in MATLAB and see what happens:

array_1 = [ 1:.2:2; 2:.2:3] array_2 = array_1(1,2:end) array_1(1, end) = 4 Change elements (1,1), (1,2), (2,1), (2,2) in one statement Change elements (1,1), (2,1), (1,5), (2,5) in one statement

In most programming languages when performing an operation on a vector, a for loop is used to loop through the entire vector, using the loop variable as the index into the vector. for i = 1:length(vec) % do something with vec(i) end % Example v=[3 7 2 1] for i = 1:length(v) v(i) =v(i) * 3; End % v=????

Matrices

09-Feb-13

Similarly, for an operation on a matrix, a nested loop would be required, and the loop variables over the rows and columns are used as the subscripts into the matrix. [r c] = size(mat); for row = 1:r for col = 1:c % do something with mat(row,col) end end

v=[1 3 2 7 4 2] max=0; for =1:length(v) if v(i)>max max=v(i); maxi=i; end end % Repeat for the minimum % Repeat for a matrix i

1 2 3 4 5 6

v(i)

1 3 2 7 4 2

max

maxi

0 1 ? 1 1 3 1 2 3 2 3 7 2 4 7 4 7 4

Matrices

09-Feb-13

Counting Elements

v=[1 3 2 -7 4 -2] c=0; for =1:length(v) if v(i)>0 c=c+1; end end % Repeat for the negative % Repeat for a matrix i

1 2 3 4 5 6

v(i)

1 3 2 -7 4 -2

c

0 1 2 3 3 4 1 2 3 4

Comparing Elements

v1=[1 3 2 7 4 -2] v2=[5 3 4 1 2 -2] for i=1:length(v1) if v1(i)>v2(i) v(i)=1; elseif v1(i)<v2(i) v(i)=-1 else v(i)=0; end end i

1 2 3 4 5 6

v1(i)

1 3 2 7 4 -2

v2(i)

5 3 4 1 2 -2

v(i)

-1 0 -1 1 1 0

Matrices

- Homework 1 KeyUploaded byNafiz Ahmed
- matlab.pdfUploaded byLuis Alberto Pernia Perdomo
- MatlabUploaded byMXR-3
- Introduzione Alla Programmazione Dei SADUploaded bypeppecol
- caplUploaded byRajesh Kumar R
- Pure MathsUploaded by林興龍
- Excel Formula s(1)Uploaded bySkulkarni104
- homework helper-grade 3 module 1Uploaded byapi-261399250
- Client Data Set in Detail7Uploaded byyc1965
- Maaa 037053Uploaded byeclecticOTA
- IFEM.Ch31Uploaded byvsbalint
- homework helper-grade 3 module 1Uploaded byapi-263076852
- Computer Applications Lecture NotesUploaded byMohamed Salah
- Designing Controller by State Space Techniques Using Reduced Order Model AlgorithmUploaded byGladiolus Tran
- Kruithof Double Factor MoreUploaded byAtique Ur Rehman Khattak
- Utility Classes 2Uploaded byapi-3749401
- 1D FEMUploaded byfake7083
- 02 GENN004 DeclarationUploaded byBishoy Emile
- NewsUploaded byLeticia Kelly
- Matrix calculatorUploaded bysuman k m
- Matrix Row OperationsUploaded byahmedmagd63
- Pages From Zhang2015Uploaded byKAS Mohamed
- Cross-Context Multi-level Interpretations to Generate Personalized RecommendationsUploaded bySyed BadShah
- 06_Chapter 6 the Plotting ClientUploaded byHumayun Nawaz
- advanced programmingUploaded byDeepanshu Sharma
- 6 Model Question Paper_assignment 2002 Lab (1)Uploaded bykushal
- B.Tech_MME_Syllabus.pdfUploaded byPreeti
- Test-1-FEM-Sem-1-20102011-finalUploaded byNadia Balqis
- Excel ReckonerUploaded byBharat Heda
- Practicle FileUploaded byJAGANNATH THAWAIT

- Mid Term CommunicationUploaded byBishoy Emile
- EulaUploaded byKotol
- Model Answer of Assignment 2 Dr Shantal - CopyUploaded byBishoy Emile
- Assignment 1Uploaded byBishoy Emile
- EconomicsUploaded byBishoy Emile
- Assignment 2 Dr ShantalUploaded byBishoy Emile
- Solved Questions on Chapter 14Uploaded byBishoy Emile
- Solved Questions on Chapter 13Uploaded byBishoy Emile
- Solved Questions on Chapter 5Uploaded byBishoy Emile
- 3. Assignment 1 Model AnswerUploaded bygeo_bi
- Model Answer ForMidterm Fall 2013Uploaded byBishoy Emile
- Model Answer for Nov 22,2011 2-bUploaded byBishoy Emile
- Model Answer for Midterm 2012Uploaded byBishoy Emile
- Assignment 1Uploaded byBishoy Emile
- InstructionUploaded byBishoy Emile
- Lab Exam - Sample 2Uploaded byBishoy Emile
- Final SampleUploaded byBishoy Emile
- prelab5Uploaded byBishoy Emile
- OPP Project 1Uploaded byBishoy Emile
- Lab_Exam_-_Sample_1.pdfUploaded byBishoy Emile
- 1 Introduction IntroductionUploaded byBishoy Emile
- Lab ExamUploaded byBishoy Emile
- Sheet5Uploaded byBishoy Emile
- Sheet6Uploaded byBishoy Emile
- Sheet4Uploaded byBishoy Emile
- Sheet7Uploaded byBishoy Emile
- Assignment (3),Uploaded byBishoy Emile
- Sheet2 (2)Uploaded byBishoy Emile
- Sheet1 (3)Uploaded byBishoy Emile

- Matrix Singular Value DecompositionUploaded byHamed Nikbakht
- Lecture 10 - Subspace FEUploaded byDinesh Kuruba
- Stock CorrelationUploaded byaidi8888
- Arkadiusz Jadczyk - Conformally Compactified Minkowski Space Myths and FactsUploaded byarkajad
- Tong quatUploaded bylenghiemckt07
- Numerical Analysis 323Uploaded bymaths203
- chap28-9Uploaded bybalakalees
- Modified.cholesky.decompositionUploaded byPino Bacada
- DeterminantsUploaded byVijaykumar Shukla
- Linear TransformationsUploaded byNealWhite
- Libro Fasshauer Numerico AvanzadoUploaded byCarlysMendozaamor
- Exercises Week 3Uploaded byFrancisco Javier Ferrer Bettín
- 11536.Engineering Maths-I(AML5101) (1)Uploaded byAbhinav Gupta
- Vector Calculus.pptUploaded byIzni Mustafar
- Critical Book Report Linear AlgebraUploaded byCindy Fildza
- Lecture 7 JacobianUploaded bypeterwalid
- Homework Assignment #1 Part OneUploaded byChala1989
- 03.00080103.clemensUploaded byAhmet Turgut
- MatrixUploaded byeminasal
- Scilab TutorialUploaded byrak_mishra109
- Chapter 3 Triple ProductUploaded byLoh Ryan Benz Ryan Loh
- Numlua Anumerical Packagefo LuaUploaded byoudet9977
- Hiptmair R., Schwab C. Numerical Treatment of PDEs. Finite Element Method (Web Draft, 2004)(223s)_MNfUploaded byapmapm
- Grad Curl Div Lap CurvilinearUploaded byhelmkrieg
- 11th BusinessMath Model PaperUploaded byShawn Parker
- 560_601_HW3Uploaded byAlwan Fauzi
- Gibbs. W. - Vector Analysis (1901).pdfUploaded byJIMMY
- ORR05_3Uploaded byManish Kukreti
- Assign2_2016A_v2Uploaded bymurster101
- An Introduction to Numerical Analysis_K. E. AtkinsonUploaded byMilos Tomic