This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
array [2015/05/06 11:51] petersen created |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== Arrays ===== | ||
| - | |||
| - | You can also create an array variable in Matlab. An array in matlab is indexed from 1 not 0. There is no zero element in matlab. Remember that every variable is a matrix in Matlab, thus an array is just a 1xn matrix. The variable y created in the Variables section is an array. The basic syntax of an Array is the following: | ||
| - | |||
| - | array_variable_name = [] | ||
| - | array_variable_name = [element_1, element_2, element_n] | ||
| - | array_variable_name = [element_1 element_2 element_n] | ||
| - | |||
| - | The first just creates an array variable without assigning it any values. The next two declarations create an array variable and assign it values. Matlab allows the user to use spaces instead of commas when separating elements in an array. | ||
| - | |||
| - | Create the following two arrays. I added a semi colon which suppresses Matlab from showing the user the variable. Don’t worry that you already have the variables x and y previously decaled. Matlab will delete the old variables and create the new ones. | ||
| - | |||
| - | x = [1 2 3]; | ||
| - | y = [4 5 6]; | ||
| - | |||
| - | Now that we have two variable created, we can cover basic arithmetic. Type x + y. You should get 5 7 9. Now type in x*y. You should get at error since every variable is treated like a matrix and you can only multiply an mxn matrix with an nxm matrix. Since x is a 1x3 matrix and y is a 1x3 matrix we cannot multiply them together, but if we transposed y we could. To transpose a matrix type a ‘ after the variable. Type the following. | ||
| - | |||
| - | x*y’ | ||
| - | You should get the result 32. | ||
| - | |||
| - | Notice that the variable y was not transposed. It is still a 1x3 matrix, but the transpose of y was created, used, and then destroyed. | ||
| - | |||
| - | It is often necessary to multiply the ith value of one array with the ith value of another array. For example, using the declared arrays x and y, I want to multiply the 1st element of each array together and the second and so on and create a new array z. That is z = [1*4 2*5 3*6]. I can do this using a simple notation ‘.’ the dot. The dot will apply a certain arithmetic operation between two arrays 1 element at a time. Type | ||
| - | |||
| - | z = x.*y | ||
| - | z should now contain the elements [4 10 18] | ||
| - | |||
| - | You can also create an array that has a specific range with specific increments. Let’s create an array that has the range from 0 to 100 with increments of 5. I could create this array using the following notation. | ||
| - | |||
| - | Array = [0:5:100] | ||
| - | The syntax is | ||
| - | Array_name = [lower bound: increment: upper bound] | ||
| - | |||
| - | This is very useful if you every need a large array with specified increments. | ||
| - | |||