CS 30 Review of Matlab for Midterm Test

Mart Molle, Spring 2016

With reference to coverage in the online textbook "Programming in MATLAB" by zyBooks

Basic Features

Text sections 1.2, 2.1-2.7, 4.10
>> 2+2
ans=
    4
>>
>> a = 2+2; b = 7, c = ...
a+b;
b =
     7
>> c
c =
    11
>>

>> x=4; y=.75; z='Hello World!';
>> class(x), class(y), class(z)
ans =
    double
ans =
    double
ans =
    char
>>
>> third=1/3; if (2*third) == (3*third - third)
'hello'
else
'goodbye'
end
ans =
    goodbye
>>

Script M-Files

Text sections 3.1, 3.2, 3.4-3.8 [I will NOT be testing you on custom functions at this time.]
>> x=input('Enter the value for x: ')
Enter the value for x: 22
x =
    22
>> name=input('What is the name of your pet? ', 's')
What is the name of your pet? Fido
name =
    Fido
>>
            >> name=input('What is the name of your pet? ', 's')
            What is the name of your pet? Fido
            name =
                Fido
            >> weight=input(['Enter ' name '''s weight in pounds: '])
            Enter Fido's weight in pounds: 22
            weight =
                22
            >>
            >> fprintf('Your pet %s weighs %d pounds.\n', name, weight)
            Your pet Fido weighs 22 pounds.
            >>

Arrays and Array Operations

Text sections 4.1-4.9, 4.12, 5.1, 8.1, 8.2, 8.5-8.9, 9.2, 9.3, 9.6, 9.10
>> size(2+2)
ans =
     1     1
>> x = 22;
>> class(x), size(x)
ans =
    double
ans =
     1     1

>> A=[ 1, 3 5], B=[ 2 4;5 6]
A =
     1     3     5
B =
     2     4
     5     6
>> class(A), size(A)
ans =
    double
ans =
     1     3
>> class(B), size(B)
ans =
    double
ans =
     2     2
 >> isempty(B)
ans =
     0
>> A=[ ]
A =
     []
>>
class(A), size(A)
ans =
    double
ans =
     0     0
>>
isempty(A)
ans =
     1
>>

>> C=[1 2 3; 4 5 6; 7 8 9]
C =
     1     2     3
     4     5     6
     7     8     9
>> C(1,1), C(3,3), C([1 3],[2 3])
ans =
     1
ans =
     9
ans =
     2     3
     8     9
>>
>> M=1, M(1), size(M)
M =
     1
ans =
     1
ans =
     1     1
>> M(2)=12
M =
     1    12
>> M(3,3)=99
M =
     1    12     0
     0     0     0
     0     0    99
>> M(20,20)
??? Index exceeds matrix dimensions.
>>
>> 1:4,  pi:2*pi,  4:2
ans =
     1     2     3     4
ans =
    3.1416    4.1416    5.1416    6.1416
ans =
   Empty matrix: 1-by-0
>> 1:2:7, 10:-5:0
ans =
     1     3     5     7
ans =
    10     5     0
>> if 1:4 == [1:2, [3 4]], 'yes', else, 'no', end
ans =
    yes
>>
>> C=[1 2 3; 4 5 6; 7 8 9]
C =
     1     2     3
     4     5     6
     7     8     9

>> C(:,1), C(1,:)
ans =
     1
     4
     7
ans =
     1     2     3
>> if C(1:end,1) == C(:,1), 'yes', else, 'no', end
ans =
    yes
>>

>> C(:)
ans =
     1
     4
     7
     2
     5
     8
     3
     6
     9
>> C(4)
ans =
     2
>> C(4:6)
ans =
     2     5     8
>>
A=[1 3 5], A(:)
A =
     1     3     5
ans =
     1
     3
     5
>>
>> C, C+1
C =
     1     2     3
     4     5     6
     7     8     9
ans =
     2     3     4
     5     6     7
     8     9    10
>> D=[2 3 4; 9 8 7; 1 1 9]
D =
     2     3     4
     9     8     7
     1     1     9
>> C+D
ans =
     3     5     7
    13    13    13
     8     9    18
>> C>D, C==D
ans =
     0     0     0
     0     0     0
     1     1     0
ans =
     0     0     0
     0     0     0
     0     0     1
>>
>> A=[1 2 3], B=[4, 5; 6 7; 8 10]
A =
     1     2     3
B =
     4     5
     6     7
     8    10
>> A*B
ans =
    40    49
>> B*A % shapes don't match
??? Error using ==> mtimes
Inner matrix dimensions must agree.
>> B*B'
ans =
    41    59    82
    59    85   118
    82   118   164

>> B'*B
ans =
   116   142
   142   174
>>
>> D, D(:)
D =
     2     3     4
     9     8     7
     1     1     9
ans =
     2
     9
     1
     3
     8
     1
     4
     7
     9
>> find(D==9), [r,c]=find(D==9)
ans =
     2
     9
r =
     2
     3
c =
     1
     3
>> D(2), D(9), D(2,1), D(3,3)
ans =
     9
ans =
     9
ans =
     9
ans =
     9
>>
>> D=[2 3 4; 9 8 7], isempty(D), size(D), size(D,1), size(D,2), length(D), numel(D)
D =
     2     3     4
     9     8     7
ans =
     0
ans =
     2     3
ans =
     2
ans =
     3
ans =
     3
ans =
     6
>> D=D(:), isempty(D), size(D), size(D,1), size(D,2), length(D), numel(D)
D =
     2
     9
     3
     8
     4
     7
ans =
     0
ans =
     6     1
ans =
     6
ans =
     1
ans =
     6
ans =
     6
>> D=[ ], isempty(D), size(D), size(D,1), size(D,2), length(D), numel(D)
D =
     []
ans =
     1
ans =
     0     0
ans =
     0
ans =
     0
ans =
     0
ans =
     0
>> D=4:2, isempty(D), size(D), size(D,1), size(D,2), length(D), numel(D)
D =
   Empty matrix: 1-by-0
ans =
     1
ans =
     1     0
ans =
     1
ans =
     0
ans =
     0
ans =
     0
>>

Character Strings

Text sections 2.9, 4.11, 7,1, 7.2, 7.5, 7.6 [we did NOT cover the "sscanf" function, but we did do "eval"]
We covered "strvcat" for making 2D arrays of characters but NOT the material of section 7.7
>> Name='John Doe', size(Name), Name(6:8)='Foo'
Name =
    John Doe
ans =
     1     8
Name =
    John Foo
>> Name(2:3)=[]
Name =
    Jn Foo
>>
>> double('a'), char(97)
ans =
    97
ans =
    a
>> double('97'), char(a)
ans =
    57    55
??? Undefined function or variable 'a'.
>>
>> num2str(2), num2str(pi), class(ans)
ans =
    2
ans =
    3.1416
ans =
    char
>> str2num('22.5'), str2num('-1'), class(ans)
ans =
   22.5000
ans =
    -1
ans =
    double
>>

Relational and Logical Operations

Text sections 2.10, 4.5, 5.2, 5.3, 11.1, 11.4 Somehow the text doesn't seem to mention "&&" and "||"

Control Flow

Text sections 11.1, 11.3, 11.5, 12.1-12.6

Cell Arrays and Structures

Text sections 14.1-14.7. 14.9
>> Bag={2, 'Hello World'; [1 2; 3 4] pi}
Bag =
    [         2]    'Hello World'
    [2x2 double]    [     3.1416]
>> class(Bag)
ans =
    cell
>>
>> Bag(2,1), class(Bag(2,1))
ans =
    [2x2 double]
ans =
    cell
>> Bag{2,1}, class(Bag{2,1})
ans =
     1     2
     3     4
ans =
    double
>>
>> animals = {'lion' 'leopard'; 'ox', 'waterbuffalo'}
animals =
     'lion'     'leopard'
     'ox'       'waterbuffalo'
>>
>> Bag{2,1}(1,2), Bag{2,1}(:,2)
ans =
     2
ans =
     2
     4
>>
>> Bag(1,1)={[2 4]} % construct a cell and place it somewhere in a cell array
Bag =
    [1x2 double]    'Hello World'
    [2x2 double]    [     3.1416]
>> Bag{1,1}=1/4 % construct a non-cell value and use it as the contents of a cell
Bag =
    [    0.2500]    'Hello World'
    [2x2 double]    [     3.1416]
>> Bag(1,1)=0 % construct a non-cell value and place it somewhere in a cell array
??? Conversion to cell from double is not possible.
>>
Bag{4,1}=12 % construct a non-cell value and place it somewhere in a cell array
Bag =
    [    0.2500]    'Hello World'
    [2x2 double]    [     3.1416]
              []               []
    [        12]               []
>>
>> clear s;
s(1).height=4;
s(2).vec(1)=1;
s(2).vec(2)=5;
s(1).name=’Fred’;
s(1).vec=[7 13];
s(2,2).height=8;
disp(s)
2x2 struct array with fields:
height
vec
name
>> size(s)
ans =
2 2
>>

Functions

Text sections 3.3, 13.1, 13.3-13.5, 13.8-13.9
function [a, b] = TryMe (c, d)
ghost = @(x) 5*x - sqrt(x);