You are on page 1of 7

Lab1

ChrisSchifer
NickSpivey
MatthewMauhar
CarlFitzpatrick

1.Createavectoroftheevenwholenumbersbetween29and73

%Startbyclearingtheworkspaceandcommandwindow
clear
clc

%Createthematrixcontainingallthenumbersbetweenthetwoinputtednumbers
X=[29:73]

%Createindexandemptysettoallowtheforlooptorunthroughallofthenumbers
i=1
Y=[]

forj=1:1:length(X)
ifround(X(j)/2)(X(j)/2)==0%Filtersouttheoddnumbers
Y(i)=X(j)%Createsthematrixwiththecorrectvalues
i=i+1%Updatesindices
end
end

disp(Y)%Displaysthematrixcontainingonlytheevennumbers

Output:

2.
%createsahorizontalvectorofwholenumberscountingby3'sfrom2to32
%andthentransposesitselfintoacolumnvector
a2=[2:(322)/10:32]'

Output:

3.
a)
%setinitialconditionsfor#3
x=[2516]
%createsanewvectoradding16toeachelementfromx
a3a=x+16
Output:

b)
%createsanewvectoradding3toeachoddindexelementfromx
a3b=x
a3b(1:2:3)=x(1:2:3)+3
Output:

c)
%createsanewvectorwhereeachelementfromxbecomesitssquareroot
a3c=sqrt(x)
Output:

d)
%createsanewvectorwhereeachelementfromxbecomesitssquare
a3d=x.^2
Output:

4.Givenx=[3157926],explainwhatthefollowingcommandsmeanbysummarizingthe
netresultofthecommand.
a)x(3)
Thisisthethirdelementofthevectorx
b)x(1:7)
Thisliststhefirstsevenelementsofx
c)x(1:end)
Liststhefirstthroughlastvaluesofx
d)x(1:end1) Liststhevaluesofxfromthefirsttothesecondtolastvalue
e)x(6:2:1)
Liststhe6thelementofxandeveryelementtwoelementsbelowthesixth
elementuntilyougettothefirstelementofx
f)x([16211])
Createsavectoroftheindicesofthelistedvalueofx
g)sum(x)
Addsallelementsofx

5.Determinewhichofthefollowingstatementswillexecuteandprovidearesult.Ifthe
commandwillnotexecute,statewhyitwillnot.
%setinitialconditionsfor#5
x=[148]
y=[215]
A=[316527]

a)
a5a=x+y
Validoutput:

b)
a5b=x+A

c)
a5c=x'+y

d)
a5d=A[x'y']

e)

%Thisdoesnotexecutebecausethematrixdimensionsdonot
%agree(cannotadd1x3and2x3matrices)
%Thisdoesnotexecutebecausethematrixdimensionsdonot
%agree(cannotadd3x1and1x3matrices)
%Thisdoesnotexecutebecausethematrixdimensions
%donotagree(cannotadd3x3and3x2matrices)

a5e=[xy']

f)
a5f=[xy]
Validoutput:

%Thisdoesnotexecutebecausethematrixdimensionsdonot
%agree(cannotconcatenate1x3and3x1matrices)

g)
a5g=A3
Validoutput:

6.Givenx=[1528901]andy=[5226002],executeandexplaintheresults
ofthefollowingcommands:
(a)x>ycomparesthevaluesofxtoy.Ifagivenvalueofxisgreaterthany,thedisplayed
outputwillbea1,ifnot,theoutputwillbea0.

Ans 0101100
(b)y<xcomparesthevaluesofytox.Ifagivenvalueofyislessthanx,thedisplayed
outputwillbea1,ifnot,theoutputwillbea0.

Ans 0101100
(c)x==ycomparesthevaluesofxtoy.Ifagivenvalueofxisequaltoy,thedisplayed
outputwillbea1,ifnot,theoutputwillbea0.

Ans 0010010
(d)x<=ycomparesthevaluesofxtoy.Ifagivenvalueofxislessthanorequaltoy,the
displayedoutputwillbea1,ifnot,theoutputwillbea0.

Ans 1010011
(e)y>=xcomparesthevaluesofytox.Ifagivenvalueofyisgreaterthanorequaltox,
thedisplayedoutputwillbea1,ifnot,theoutputwillbea0.

Ans 1010011
(f)x|yaskstheprogramifeithernumbersinthetwomatricesarenonzero.Ifso,theoutput
is1,andifnot,theoutputis0.

Ans 1111101
(g)x&yaskstheprogramifbothnumbersinthetwomatricesarenonzero.Ifso,theoutput
is1,andifnot,theoutputis0.

Ans 1111001
(h)x&(y)askstheprogramifxisisanonzeronumberANDyisnotanonzeronumber,
thereforezero.Ifso,theoutputis1,andifnot,theoutputis0.


Ans 0000100
(i)(x>y)|(y<x)askstheprogramifxisgreaterthany,orifyislessthanx(thesame
thing).Ifso,theoutputis1,andifnot,theoutputis0.

Ans 0101100

(j)(x>y)&(y<x)askstheprogramifxisgreaterthany,andifyislessthanx(again,the
samething).Ifso,theoutputis1,andifnot,theoutputis0.

Ans 0101100

7.
a)(x<3)&(x<8)
Listsavalueof1intheindicesofthexvectorbetween3and8and0fortherest
Output00011111000
b)x(x>5)
Liststheindicesofthexvectorforthevaluesofx>5inthexvector(Liststhe6ththrough
10thindicesofthexvector)
Output678910
c)y(x<=4)
Liststheindicesoftheyvectorforthevalueswherex<=4(Liststhe1stthrough4thindicesof
theyvector)
Output3156
d)x((x<2)|(x>=8))
Liststheindicesofthexvectorforthevaluesofthexvectorwherexislessthan2orgreater
than8(liststhe1st,9th,and10thindicesofthexvector)
Output18910
e)y((x>=3)&(x<6))
Liststhevaluesoftheyvectorfortheindiceswherexis>=3andx<6(i.e.the3rd,4th,and5th
indicesoftheyvector)
Output568

8.
%setinitialconditionsfor#8
x=[3159121012961]
a)
a8a=x
%foreachelementinx,iftheelementisnegative,theelementbecomes
%oneinthenewmatrix,otherwisetheelementremainsthesameasthe
%xelement
fori=1:length(x)
ifa8a(i)<0

a8a(i)=1
end
end

a8a
Output:

b)
%foreachelementinx,iftheelementiseven(checkedbytheremainder
%function)thenthatvalueismultipliedby5inthenewmatrix,otherwise
%theelementremainsthesameasthexelement
a8b=x
fori=1:length(a8b)
ifrem(a8b(i),2)==0

a8b(i)=a8b(i)*5
end
end
a8b
Output:

c)
%foreachelementinthexmatrix,iftheelementis>10,thenthat
%elementisplacedintothematrixy.Nootherelementsareiny.
a8c=x
y=[]
w=1
fori=1:length(a8c)
ifa8c(i)>10

y(w)=a8c(i)

w=w+1
end
end
y
Output:

d)
%foreachelementinx,iftheelementislessthanthemean(checkedbythe
%meanfunction),theelementbecomeszerointhenewmatrix,otherwisethe

%elementremainsthesameasthexelement
a8d=x
fori=1:length(a8d)
ifa8d(i)<mean(a8d)

a8d(i)=0
end
end
a8d
Output:

You might also like