SUNY Geneseo Department of Mathematics
Friday, February 5
Math 230 02
Spring 2016
Prof. Doug Baldwin
linspace
and logspace
also create vectors>> letters = [ 7, 5, 6 ]
letters =
7 5 6
>> letters(1)
ans =
7
>> letters(3) = letters(3) - 2
letters =
7 5 4
>> table = [1 2 3; 10 20 30]
table =
1 2 3
10 20 30
>> table(1,2)
ans =
2
>> table(4)
ans =
20
>> % Leap years from 2000 to 2099
>> [2000 : 4 : 2099]
ans =
Columns 1 through 5
2000 2004 2008 2012 2016
Columns 6 through 10
2020 2024 2028 2032 2036
Columns 11 through 15
2040 2044 2048 2052 2056
Columns 16 through 20
2060 2064 2068 2072 2076
Columns 21 through 25
2080 2084 2088 2092 2096
>> [2099 : -4 : 2000]
ans =
Columns 1 through 5
2099 2095 2091 2087 2083
Columns 6 through 10
2079 2075 2071 2067 2063
Columns 11 through 15
2059 2055 2051 2047 2043
Columns 16 through 20
2039 2035 2031 2027 2023
Columns 21 through 25
2019 2015 2011 2007 2003
>> [2096 : -4 : 2000]
ans =
Columns 1 through 5
2096 2092 2088 2084 2080
Columns 6 through 10
2076 2072 2068 2064 2060
Columns 11 through 15
2056 2052 2048 2044 2040
Columns 16 through 20
2036 2032 2028 2024 2020
Columns 21 through 25
2016 2012 2008 2004 2000
>> % Table of squares of numbers from 1 to 10
>> % ...with exponentiation
>> [1:10] .^ 2
ans =
1 4 9 16 25 36 49 64 81 100
>> linspace(1,10,10) .^ 2
ans =
1 4 9 16 25 36 49 64 81 100
>> [1:10] ^ 2
{Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.}
>> %... with multiplication
>> [1:10] .* [1:10]
ans =
1 4 9 16 25 36 49 64 81 100
>> [1:10] * [1:10]
{Error using *
Inner matrix dimensions must agree.}
>> % Calculate hypotenuses of many triangles at once (after single-triangle example)
>> a = 3; b = 4;
>> sqrt( a^2 + b^2 )
ans =
5
>> as = [ 3, 5, 73.6 ];
>> bs = [ 4, 12, 9];
>> sqrt( as .^ 2 + bs .^ 2 )
ans =
5.0000 13.0000 74.1482