A2 Assignment, indexing, and variable types
Notice the difference between ending a line with a semicolon and not. For example, the second line in the list above, which did not end in a semicolon, produced an output:
>> x*y'
ans =
44
Ending an expression with a semicolon suppresses output, which is useful when writing a long list of connected expressions and you are only interested in the final output. In a very simple example, you might want to manually reproduce the calculations in this matrix multiply:
>> a=x(1)*y(1);
>> b=x(2)*y(2);
>> c=x(3)*y(3);
>> d=[a b c];
>> sum(d)
ans =
44
There is a single output (answer) because we have a semicolon that suppresses the outputs on all but the last line. The first line assigns a new variable, a, the value 2. We could have had the same result by writing:
>> a=2;
We wrote this assignment the way we did to highlight the process of indexing, which means to select a part of a matrix by referring to its position. The first step to perform the matrix multiply is to find the product of the first elements of the x matrix and the y matrix. We next need the product of the second elements of x and y, which we assign to the variable b, and we assign the product of the third elements to the variable c. Finally, we assign the list of a, b, and c values to the variable d and ask for the sum of d as our output. Indexing can also be used to select sections of a matrix. Let’s make a new matrix:
>> D=[1 2 3; 4 5 6; 7 8 9];
This is a 3x3 matrix (and is distinct from the variable d, defined above: Matlab is case-sensitive). Now let’s try selecting portions of the matrix:
>> D(1:3,1)
>> D(2,1:3)
>> D(1:2,2:3)
You can see that the colon acts as a shorthand: it asks for the list of whole numbers that starts with the number before the colon and ends with the number after the colon. An even shorter shorthand can be used to ask for all the numbers that index a particular dimension of a matrix:
>> D(:,1)
>> D(2,:)
>> D(:)
The first two of these just repeat the longer-form indexing above (1:3). The last expression does something different. It asks for all the indices from all the dimensions of the matrix. We can also replace one of the matrix elements by putting a new value into an indexed position in the D matrix:
>> D(3,1)=0
Until now, all of our variables have been matrices (including 1x1 matrices). We created these matrices by writing numbers contained in square brackets. The second variable type with which we will want to become familiar is the cell. You can think of a cell as ‘a bag of stuff’. It can contain numbers, letters, words, matrices, or even other cells. Let’s create one and see how it works:
>> c=cell(2,3);
>> c
First, notice that we over-wrote and erased the earlier c variable when we assigned the empty 2x3 cell to this variable name. Matlab doesn’t warn you at all when you over-write variables, so be careful and keep track of your variable names. Second, take a look at the new cell variable and notice that each of the cells in the cell array is contained in curly brackets. Indexing a cell array is two-tiered: first you indicate the cell, and then the position within the cell. So let’s fill our cell array with some ‘stuff’ and give it a try:
>> c{1,1}=rand(2,2);
>> c{1,2}=rand(3);
>> c{1,3}=rand(4);
>> c{2,1}=cell(2);
>> c{2,2}={[],[]};
>> c{2,3}={'January','February','March'};
>> c
Now take a look at each cell in the array:
In the first row, we’ve filled each cell with a matrix. You can look at what is in each cell by indexing within a curly bracket:
>> c{1,1}
>> c{1,2}
>> c{1,3}
and within each cell, you can select parts of each matrix by adding matrix indexing as above:
>> c{1,1}(1,:)
>> c{1,2}(2,2:3)
>> c{1,3}(2:3,3:4)
In the second row we’ve filled each cell with another cell. The first element of the second row is an empty 2x2 cell. The second element is filled with an empty 1x2 cell, but notice we’ve asked for these empty cells in two different ways. We can fill these cells-within-cells in the way you might have guessed: by referring to the appropriate double-curly-index. Let’s put something in each of these empty cells:
>> c{2,1}{1,1}=zeros(2);
>> c{2,1}{1,2}=zeros(1,3);
>> c{2,1}{2,1}=ones(3);
>> c{2,1}{2,2}=3*ones(2,3);
>> c{2,2}{1}=ones(3,1)*[3 4 5 6];
>> c{2,2}{2}=[1 2 3]'*ones(1,4);
Now go ahead an check the contents of this second row of cells:
>> c{2,1}
>> c{2,1}{1,1}
>> c{2,1}{2,2}
>> c{2,2}{1}
>> c{2,2}{2}
>> c{2,3}