% Jake Bobowski % June 6, 2016 % Created using MATLAB R2014a clearvars % Lists are created using square brackets. xData=[1,2,5,12,-3,3.4,pi,19,-12] % You can use 'length' to check the number of elements in a list. length(xData) % Here's another list. yData=[2,3,4,5,5,4,3,2,4]; length(yData); % A list can be transposed (from a row to a column and vise-versa) using % the ' notation as follows: xColumn = xData' xRow = xColumn' % A specific element of a list can be selected using (). xData(3) % Here's how to select a multiple values and a range of values: xData([2,4,7]) xSubset = xData(3:7) % All of these operations work for the column data too. Here's how to % select the elements from 4 to the end of the list: xColumn(4:length(xColumn)) % If you try to square a list, as in xData^2, you'll get an error. The % error occurs because the square of a list is ambiguous. You might mean % 'make a new list where each of it's elements is the square of the % elements in the original list', or you might mean 'take the dot product of % xData with itself where each elements of the list is like a component of % an N-dimensional vector (assuming the xData has N elements)', or you % might mean something like a cross product. % There are two ways to do an element-by-element square. The first uses % the 'power' command and the second using the '.' notation before the % operator. xSquare = power(xData,2) xSquare = xData.^2 % Of course it works for other power too. power(xData,3.4) xData.^-2.3 % If you have a pair of lists of equal lengths, the '.' notation can be % used for element-by-elemnet products and quotients. list1 = [1,3,45,5,-4.3,-2]; list2 = [-3,3.33,4.1,-1.2,-0.85,1]; length(list1) length(list2) % Element-by-element product: list1.*list2 % Element-by-element quotient: list1./list2 % Here's the dot product of list1 and list2: dot(list1,list2) % and the cross product of a pair of 3-D vectors. Notice that a x b equals % -b x a as expected. Also notice that the cross product produces a vector % output (also as expected). cross([1,2,3],[4,5,6]) cross([4,5,6],[1,2,3]) % MATLAB has a function that will generate n linearly spaced numbers % between the limits a and b called linspace(a,b,n)/ linspace(1,954,23) % You can also generate n logarithmically spaced numbers between 10^a and % 10^b using logspace(a,b,n). logspace(-2,2,12)
xData =
Columns 1 through 7
1.0000 2.0000 5.0000 12.0000 -3.0000 3.4000 3.1416
Columns 8 through 9
19.0000 -12.0000
ans =
9
xColumn =
1.0000
2.0000
5.0000
12.0000
-3.0000
3.4000
3.1416
19.0000
-12.0000
xRow =
Columns 1 through 7
1.0000 2.0000 5.0000 12.0000 -3.0000 3.4000 3.1416
Columns 8 through 9
19.0000 -12.0000
ans =
5
ans =
2.0000 12.0000 3.1416
xSubset =
5.0000 12.0000 -3.0000 3.4000 3.1416
ans =
12.0000
-3.0000
3.4000
3.1416
19.0000
-12.0000
xSquare =
Columns 1 through 7
1.0000 4.0000 25.0000 144.0000 9.0000 11.5600 9.8696
Columns 8 through 9
361.0000 144.0000
xSquare =
Columns 1 through 7
1.0000 4.0000 25.0000 144.0000 9.0000 11.5600 9.8696
Columns 8 through 9
361.0000 144.0000
ans =
1.0e+04 *
Columns 1 through 4
0.0001 + 0.0000i 0.0011 + 0.0000i 0.0238 + 0.0000i 0.4669 + 0.0000i
Columns 5 through 8
-0.0013 - 0.0040i 0.0064 + 0.0000i 0.0049 + 0.0000i 2.2272 + 0.0000i
Column 9
-0.1443 - 0.4440i
ans =
Columns 1 through 4
1.0000 + 0.0000i 0.2031 + 0.0000i 0.0247 + 0.0000i 0.0033 + 0.0000i
Columns 5 through 8
0.0470 - 0.0647i 0.0599 + 0.0000i 0.0719 + 0.0000i 0.0011 + 0.0000i
Column 9
0.0019 - 0.0027i
ans =
6
ans =
6
ans =
-3.0000 9.9900 184.5000 -6.0000 3.6550 -2.0000
ans =
-0.3333 0.9009 10.9756 -4.1667 5.0588 -2.0000
ans =
187.1450
ans =
-3 6 -3
ans =
3 -6 3
ans =
Columns 1 through 7
1.0000 44.3182 87.6364 130.9545 174.2727 217.5909 260.9091
Columns 8 through 14
304.2273 347.5455 390.8636 434.1818 477.5000 520.8182 564.1364
Columns 15 through 21
607.4545 650.7727 694.0909 737.4091 780.7273 824.0455 867.3636
Columns 22 through 23
910.6818 954.0000
ans =
Columns 1 through 7
0.0100 0.0231 0.0534 0.1233 0.2848 0.6579 1.5199
Columns 8 through 12
3.5112 8.1113 18.7382 43.2876 100.0000