Sunteți pe pagina 1din 3

9/7/2015

Lecture2

Lecture2
AccessingMatrixElements
Submatrices
Wealreadysayinthepreviouslecturethatyoucanaccessthe(2,3)thelementofamatrixa
withthenotation

>>a(2,3)
Thisgivestheentryofainthesecondrowandthethirdcolumn.

Wecanalsogainaccesstoseveralelementsatthesametime.Tryforexample:
>>a(2,[12])
>>a([34],3)
>>a([23],[34])
Thesecommandsextractasubmatrixofawiththecorrespondingsetofrowsandcolumns.
Itisoftenusefultoextractawholeroworcolumnofagivenmatrix.AssumingthatBhas10
rows,wecanextractthesecondcolumnofBwiththecommand
>>B(2,1:10)
Usingthecolumnnotationinsteadofspellingout[12345678910].
Infact,thereismoreshorthandtobelearnedhere.First,wecanreplacethe10withthe
keywordend:
>>B(2,1:end)

Thiskeywordgetsreplacedbythelargestnumberallowedforthatplace.Soifitisusedinthe
rowsitwillbereplacedwitthenumberofrows,ifinthecolumns,withthenumberof
columns.Itisusefultoknowthatonecanmanipulateitasonewouldanyothervariable:
>>B(2,1:end1)
>>B(2,1:end/2)
Ofcourse,ifbythemanipulationyougetanillegalexpression,matlabwillcomplain:

>>B(2,1:end+1)
>>B(2,sin(end):end)
The1:endconstructionissousefulthatMatlabhasafurthershorthandforit:
>>B(2,:)

MeansthesameasB(2,1:end).So,oneshouldthinkofthebarecolon(:)asmeaning
http://ocw.mit.edu/ans7870/resources/farjoun/Lecture2.html

1/3

9/7/2015

Lecture2

"everything".

Noncontiguouselementaccess
Theelementsofthematrixthatweaccessdonothavetobecontiguous:

>>a(3,[246])
>>a([24],[13])
Theydon'thavetobeinincreasingorder:

>>a(3,[264])
>>a([41],[32])
Nordotheyhavetoonlyappearonce:

>>a(3,[2244111])
>>a(3*ones(1,3),2*ones(1,10))

Exercises:
Verifythatthesumalongtheantidiagonalofthemagicmatrixisalsothesameasall
theotherrows,colsandthediagonal.(Hint:youcanfirstchangetheorderoftherows
orcolumnsbeforetakingthediag)
IfAisamatrix,howcanyougetthesubmatrixofelementswhose
bothcoordinatesareodd?
Letx=[2516]andy=[4213].Thinkofyasaspecificreorderingof
thenumbers1:4.Useytoreorderxinthesamefashion.
Givena``permutation''vectorasyisinthepreviousitem.Findthevectorwhichgives
theinverseofthepermutation!(Tricky!)
Giventwopermutationspandq,findthepermutationsthatisthesameasp(q(s))(thatis
itpermutesthesetsasifyouwouldfirstpermutewithqandthenwithp)

Assigningintosubmatrices
Justasinthereferenceandassignmentofsinglenumbersintomatrices,wecanalsoassign
newvaluesintocompletesubmatrices.Theonlythingthatneedstobecheckedisthatthe
matrixontherightoftheequalsignhasthesamedimensionasthematrixreferencedonthe
left:
>>a([23],[14])=[1234]
>>a(1,1:4)=2:5

Scalarexpansion
Matlabhasanicetimesavingnotation.Ifanoperatorrequiresamatrixofaknownsizeand
http://ocw.mit.edu/ans7870/resources/farjoun/Lecture2.html

2/3

9/7/2015

Lecture2

insteadisgivenascalar(i.eanumber).It"expands"thescalarintoafullmatrixofthe
requiredsize.Thisisquitecumbersometosay...muchbettertoshowexamples:
>>1:10.^2
>>2.^1:10
>>[1234]+5
Inthesethreecases,therelevantoperatorislookingforamatrixofthesamesizeastheoneit
alreadyhasontheotherside,butinsteaditfindsanumber.SoMatlabpretendsasifthematrix
oftheexpectedsizeistherewitheachofitsentriesequaltothenumber.

Exercises
Writeanexpressionforthesumoftheintegersfrom1to100
Writeanexpressionforthesumofthesquaresofthenumbersfrom1to10
Writeanexpressionforthesumofthepowersof0.5tothenumbersfrom1to10
Letx=[2516].Add3tojusttheoddindexelements(resultingina2vector),
adds3tothemandputstheresultintheevenpositionsoftheoriginalvector
(overwritingthe2andthe1).

http://ocw.mit.edu/ans7870/resources/farjoun/Lecture2.html

3/3

S-ar putea să vă placă și