You are on page 1of 4

EmptyMatrices,Scalars,andVectors

Overview
Althoughmatricesaretwodimensional,theydonotalwaysappeartohavearectangularshape.A1by8matrix,for
example,hastwodimensionsyetislinear.Thesematricesaredescribedinthefollowingsections:

TheEmptyMatrix
Anemptymatrixhasoneormoredimensionsthatareequaltozero.Atwodimensionalmatrixwithbothdimensions
equaltozeroappearsintheMATLABapplicationas[].TheexpressionA=[]assignsa0by0emptymatrixtoA.

Scalars
Ascalaris1by1andappearsinMATLABasasinglerealorcomplexnumber(e.g.,7,583.62,3.51,5.46097e14,
83+4i).

Vectors
Avectoris1bynornby1,andappearsinMATLABasaroworcolumnofrealorcomplexnumbers:
ColumnVectorRowVector
53.253.287.39412i43.9
87.39
412i
43.9

TheEmptyMatrix
Amatrixhavingatleastonedimensionequaltozeroiscalledanemptymatrix.Thesimplestemptymatrixis0by0insize.
Examplesofmorecomplexmatricesarethoseofdimension0by5or10by0.
Tocreatea0by0matrix,usethesquarebracketoperatorswithnovaluespecified:
A=[];
whosA
NameSizeBytesClassAttributes
A0x00double
Youcancreateemptymatrices(andarrays)ofothersizesusingthezeros,ones,rand,oreyefunctions.Tocreatea0by
5matrix,forexample,use
A=zeros(0,5)
A=
05emptydoublematrix
OperatingonanEmptyMatrix
Thebasicmodelforemptymatricesisthatanyoperationthatisdefinedformbynmatrices,andthatproducesaresult
whosedimensionissomefunctionofmandn,shouldstillbeallowedwhenmorniszero.Thesizeoftheresultofthis
operationisconsistentwiththesizeoftheresultgeneratedwhenworkingwithnonemptyvalues,butinsteadisevaluatedat
zero.
Forexample,horizontalconcatenation
C=[AB]
requiresthatAandBhavethesamenumberofrows.SoifAismbynandBismbyp,thenCismby(n+p).Thisisstill
trueifm,n,orpiszero.

AswithallmatricesinMATLAB,youmustfollowtherulesconcerningcompatibledimensions.Inthefollowingexample,an
attempttoadda1by3matrixtoa0by3emptymatrixresultsinanerror:
[123]+ones(0,3)
Errorusing+
Matrixdimensionsmustagree.
CommonOperations.Thefollowingoperationsreturnzeroonanemptyarray:
A=[];
size(A),length(A),numel(A),any(A),sum(A)
Theseoperationsreturnanonzerovalueonanemptyarray:
A=[];
ndims(A),isnumeric(A),isreal(A),isfloat(A),isempty(A),...
all(A),prod(A)
UsingEmptyMatricesinRelationalOperations
Youcanuseemptymatricesinrelationaloperationssuchas"equalto"(==)or"greaterthan"(>)aslongasbothoperands
havethesamedimensions,orthenonemptyoperandisscalar.Theresultofanyrelationaloperationinvolvinganempty
matrixistheemptymatrix.Evencomparinganemptymatrixforequalitytoitselfdoesnotreturntrue,butinsteadyieldsan
emptymatrix:
x=ones(0,3);
y=x;
y==x
ans=
03emptylogicalarray
UsingEmptyMatricesinLogicalOperations
MATLABhastwodistincttypesoflogicaloperators:

Shortcircuit(&&,||)Usedintestingmultiplelogicalconditions(e.g.,x>=50&&x<100)whereeachcondition
evaluatestoascalartrueorfalse.

Elementwise(&,|)PerformsalogicalAND,OR,orNOToneachelementofamatrixorarray.

ShortcircuitOperations.Theruleforoperandsusedinshortcircuitoperationsisthateachoperandmustbeconvertible
toalogicalscalarvalue.Becauseofthisrule,emptymatricescannotbeusedinshortcircuitlogicaloperations.Such
operationsreturnanerror.
TheonlyexceptionisinthecasewhereMATLABcandeterminetheresultofalogicalstatementwithouthavingtoevaluate
theentireexpression.Thisistrueforthefollowingtwostatementsbecausetheresultoftheentirestatementsareknownby
consideringjustthefirstterm:

true||[]
ans=
logical
1
false&&[]
ans=
logical
0
ElementwiseOperations.Unliketheshortcircuitoperators,allelementwiseoperationsonemptymatricesare
consideredvalidaslongasthedimensionsoftheoperandsagree,orthenonemptyoperandisscalar.Elementwise
operationsonemptymatricesalwaysreturnanemptymatrix:
true|[]
ans=
00emptylogicalarray
NoteThisbehaviorisconsistentwiththewayMATLABdoesscalarexpansionwithbinaryoperators,whereinthenonscalar
operanddeterminesthesizeoftheresult.

Scalars
AnyindividualrealorcomplexnumberisrepresentedinMATLABasa1by1matrixcalledascalarvalue:
A=5;
ndims(A)%ChecknumberofdimensionsinA
ans=
2
size(A)%Checkvalueofrowandcolumndimensions
ans=
11
Usetheisscalarfunctiontotellifavariableholdsascalarvalue:
isscalar(A)
ans=
logical
1

Vectors
Matriceswithonedimensionequaltooneandtheothergreaterthanonearecalledvectors.Hereisanexampleofa
numericvector:

A=[5.7324i9/725e3.046sqrt(32)8j];
size(A)%Checkvalueofrowandcolumndimensions
ans=
17
Youcanconstructavectoroutofothervectors,aslongasthecriticaldimensionsagree.Allcomponentsofarowvector
mustbescalarsorotherrowvectors.Similarly,allcomponentsofacolumnvectormustbescalarsorothercolumnvectors:
A=[294377921];
B=[04611];
C=[A5ones(1,3)B]
C=
294377921511104611
Concatenatinganemptymatrixtoavectorhasnoeffectontheresultingvector.Theemptymatrixisignoredinthiscase:
A=[5.36;7.01;[];9.44]
A=
5.3600
7.0100
9.4400
Usetheisvectorfunctiontotellifavariableholdsavector:
isvector(A)
ans=
logical
1

You might also like