Вы находитесь на странице: 1из 8

ManipulatingMatriceswithMATLAB

Ref.MooreCh.4
Matricescanbegeneratedusingthearraygenerationsyntax
Comasseparaterowentriesandsemicolonsseparatecolumnentries.
e.g.C=[1,0,0;1,1,0;0,0,2]
>>C=[1,0,0;1,1,0;0,0,2]
C=
100
110
002
oralternativelythesamematrixcanbegeneratedasfollows:
>>C=[1,0,0;
1,1,0;
0,0,2]
C=
100
110
002
Useofellipsistoindicatecontinuationofarowinmatrixgeneration.p.122
>>F=[1,2,3,4,5...
6,7,8,9]
F=
1|P a g e

123456789
Matrixwithinamatrix.
P=[1.5,3.2,6.3];
Q=[4.2,8.1,P];

WhatwouldbestoredinQ?
WhatisthedimensionofQ?

>>length(Q)
>>length(P)

DisplayingtheindividualentriesofarrayQ
>>Q(3)
etc

Weencounteredthecolonoperatorbefore
Determinetheoutcomeofthefollowingarrays
>>R=4:13
>>R=4:1:13
>>R=4:2:13

2|P a g e

Extractionofrowsorcolumnsfromamatrix
UsematrixMasshownonp.123
>>M=[1,2,3,4,5;2,3,4,5,6;3,4,5,6,7];
Extractcolumn2fromthe3X5matrix
>>col2=M(:,2)
col2=
2
3
4
Extractthethirdrow
>>row3=M(3,:)
row3=
34567

Extractalltherowsofbothrow2androw3ofthematrix

>>bot2=M(2:3,:)
bot2=
23456
34567

3|P a g e

Extractthesubmatrixcontainingrow2torow3,column4column5
>>sub1=M(2:3,4:5)
sub1=
56
67

ExtractindividualentriesasM(r,c)
>>M(2,4)
ans=
5
>>M(3,2)
ans=
4

LoadingandmanipulatingmatricescreatedusingEXCEL

LoadamatrixnamedAsheville_1999.xlsxintoMATLABbyclickingonitsiconfrom
thecommandwindow
ExtendthehighlighteddataonlyrangetoincludecolumnJ
ClickImportDatabelowtheImportSelectionicon
Onthecommandwindow,typewhostoidentifytheidentityoftheimporteddata

4|P a g e

>>whos
NameSizeBytesClassAttributes

Highest12x196double
Lowest12x196double
MeanMax12x196double
MeanMin12x196double
Month12x196double
columnD12x11344cell
columnE12x11344cell
columnF12x11344cell
columnG12x11344cell
columnI12x11344cell

Columnswithnonzerodatainthem,havebeenimportedandassignedthe
variablenamethatwasatthetopofthatdatacolumn,henceMonth,MeanMax,
MeanMin,HighsetandLowest.
Allintermediatecolumnsareidentifiedasplaceholdersonly

Nowthatthedataisloadedyoucanproceedwiththecomputationsofnumerical
valuesassociatedwiththeimporteddataaccordingtoexample4.1p.126128

5|P a g e

Problemsinvolvingtwovariables
Scalaroperations
x=4,y=7
A=x*yisastraightforwardcommandlinecomputation

Scalaronarrayoperations
>>x=1:6;
>>y=5;
>>B=x*y
B=
51015202530
Thetermbytermmultiplicationiscompletedwithoutthearrayoperation
Notethatusingthearrayoperation.*herealsoworksbutisnotnecessary.

Scalaronscalaroperations
>>x=1:5;
x=1,2,3,4,5
>>y=linspace(1,3,5);
y=1,1.5,2,2.5,3
>>A=x.*y
A=
1361015
6|P a g e

TryA=x*b

Useofthemeshgridfacility
Supposeweneedtogenerateatermbytermmultiplicationofallvaluesofxon
allvaluesofy.Meshgridisaconvenienttoolforthis.
>>[x1,y1]=meshgrid(x,y)
x1=
12345
12345
12345
12345
12345
y1=
1.00001.00001.00001.00001.0000
1.50001.50001.50001.50001.5000
2.00002.00002.00002.00002.0000
2.50002.50002.50002.50002.5000
3.00003.00003.00003.00003.0000

7|P a g e

Computethematrixofthetermbytermmultiplicationsofx1byy1
>>AA=x1.*y1
AA=
1.00002.00003.00004.00005.0000
1.50003.00004.50006.00007.5000
2.00004.00006.00008.000010.0000
2.50005.00007.500010.000012.5000
3.00006.00009.000012.000015.0000

CasestudiesfortheapplicationofmeshgridaregivenasillustrationsofPractice
Examples4.2and4.3

Specialmatricesreview,table4.3p.136

8|P a g e

Вам также может понравиться