


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



doub1=3.0;doub2=1; str1='hello';str2='h'; boo1=false;boo2=5>2;
r1=[1,2,3,4,5]%Commas to separate entries in an array within a row. r2=1:2:9%This is equivalent to writing r2=[1,3,5,7,9]. The notation is start:step:stop. %A notation start:stop can also be used, which simply means start:1:stop. c1=[1;2;3;4;5]%semicolons to separate rows. c2=(1:2:9)'%The ' symbol means "transpose", i.e., switch the role of rows and columns. M1=[1,2,3;4,5,6;7,8,9]%M1 is a 2-dimensional array.
r2(2)%Gives 3, since the second entry in r2 is 3. c2(4)%Gives 7, since the fourth entry in r2 is 3. M1(3,2)%Gives 8, since M1(3) is [7,8,9] and 7,8,9 is 8.
rbig=cat(2,r1,r2)%cat(2,array1,array2) concatenates array1 and array2 row-wise. %This gives rbig=[1,2,3,4,5,1,3,5,7,9]. cbig=cat(1,c1,c2)%cat(1,array1,array2) concatenates the two specified arrays column-wise. %This gives cbig=[1;2;3;4;5;1;3;5;7;9]. r2small=r2(2:4)%r2small=[3,5,7].
2+3%outputs 5. 23%outputs 6. r1+r1%outputs [2,4,6,8,10]. r1r1%outputs error, since dimensions aren't consistent for matrix multiplication. r1.r1%outputs [1,4,9,16,25] by element-wise multiplication. r1c1%outputs 55, since the matrix multiplication gives 11+22+33+44+55= c1r1%outputs [1,2,3,4,5;2,4,6,8,10;3,6,9,12,15;4,8,12,16,20;5,10,15,20,25], %since this is what the matrix multiplication gives. [1,1;1,1]^2%outputs [2,2;2,2] by matrix multiplication. [1,1;1,1].^2%outputs[1,1;1,1] by element-wise exponentiation.
4==22%evaluates to true, since 4=22. 4==23%evaluates to false, since 23 does note equal 4. 4>2%evaluates to true, since 4 is greater than 2. 2<2%evaluates to false, since 2 is not less than itself. 2<=2%evaluates to true, since 2 is less than or equal to itself. 4 ~=2%evaluates to true, since 4 is not equal to 2. 2 ~=2%evaluates to false, since 2 is not not equal to itself. isequal(M1,M1)%evaluates to false, since M1 is the same array as itself. isequal(r1,r2)%evaluates to false, since these two arrays are different.
true&true%outputs true. true&false%outputs false. false&true%outputs false. false&false%outputs false. true|true%outputs true. true|false%outputs true. false|true%outputs true. false|false%outputs false. ~true%outputs false. ~false%outputs true. 2<2|4>2%outputs true, since this is the same as false|true. ~2<3%outputs false, since this is the same as ~true. (2<2|4>2)&(~2<3)%outputs false, since this is the same as true&false.
sin(x) %takes x as an input and gives the sine of x as an output. isequal(x,y) %takes two doubles x and y as inputs and gives true or false as an output, %depending on whether they are equal. plot(x,y) %takes two arrays x and y as inputs and performs a task, namely, plots y against x.
for LoopVariable=start:step:stop%if step=1, format is for LoopVariable=start:stop body%executes, starting with LoopVariable=start, ending when LoopVariable>stop. end
while (statement that can evaluate to true or false)%Parentheses not necessary body end
function sqArray = sqArrayFor(low,high)%for version arr=[]; for i=low:high arr=cat(2,arr,i^2); end sqArray=arr; end
function sqArray = sqArrayWhile(low,high)%while version i=low; arr=[]; while (i<=high) arr=cat(2,arr,i^2); i=i+1; end sqArray=arr; end
%Save this script as MySquareArray.m. Once this is done, can run it by typing %"run MySquareArray.m" in the Command Window. %Note that script assigns values to variables just as the Commmand Window does. %Note also that it has access to functions that you have written. a=3;b=5; MyArray=sqArrayFor(a,b)