Wednesday, October 22, 2008

Some more Basic Material


Some more Useful Stuff

Well, just when I thought I had covered most of the basic stuff we might need, I am reminded that there are few things that I forgot to mention. Here is a quick list:

1. While I mentioned that MATLAB accepts only letters and alphabets in variable names, I think it must also be mentioned that it does not accept any special character except underscore (_). Also,  only first 31 characters are recognized (though why one would need a variable name that is more than 31 characters long is any one's guess....)

2. It's best not to use reserved names for variables. For example, although one can use the name pi to denote a variable, pi is also an inbuilt variable which takes the value 3.142...Similar warning applies for other inbuilt variables like ans, NaN, Inf etc.

3. save can be used to only save select number of variables in the workspace. Just like clear, save myfilename saves all the variables in a workspace to the .mat file myfilenname. save myfilename x1 x2, however, only saves variables x1 and x2 to the file.

4. diary('diaryname') saves the diary in the file diaryname. As earlier mentioned, however, diary works like a switch. You can switch it on / off, just by using the command diary.

5. You can use the command workspace to open the workspace window if it's closed. Try.

6. You can open a variable in an excel like window by using the command open varname (you can edit there too).

7. We use the command who to see variables in a workspace. Similarly we use the command what to see what .mat files are there in the MATLAB directory. which lists the path where an in-built function is stored (for example try which mean).

8. end: An immensely useful command to access the end of a column. For example:

>> x(3 : end);

% gives the 3rd to last element of the vector

>> x(1, 1 : 2 : end - 1);  

% as you can see it can be operated upon too

Just a precaution, when using end, to grow an array, as in x(end + 1) = 5, make sure x exists first. In this case the vector is appended by the value 5 (at the bottom) ...so now x(end)  = 5.

9.[]: represents an empty matrix (with nothing in it, but where we can put something...like an empty basket, if you will). It's very useful to concatenate elements when running a loop (more on this later). Another use of this is to delete an entire row or column of a matrix. This is how it works:

x(:, 2) = [];

% deletes the second column

x(2, :) = [];

% deletes the second row

10. Interrupting a running program: Ctrl-C (equivalent to task manager -> end task)

11. You can extend what you are writing on to another line by using ellipsis: ...

12. Other useful functions like sum etc include: range, abs

13. prctile: prctile(x, p) returns a value that is greater than p% of the values in x. So p = 50 is equivalent to median. Applied on a matrix, this returns a row vector, with the pth percentile of each column separately.

14. Concatenation: Well, as it happens there is a command for this too. So

[A B] can also be done using cat(2, A, B)

[A; B] can also be done using cat(1, A, B)

I never knew these functions existed till now, but they can be very useful in long programs. It's harder to mis-interpret cat(2, A, B) than [A B] (when you have long programs with lots of transformations and extensions or reductions of matrices, this can make debugging easier).

15. prod, cumsum, cumprod, diff: Again, just like sum, prod multiplies all the elements in a column. And typically, for a matrix it returns a row vector. cumsum as the name suggests gives a cumulative sum for a column. So, cumsum([1 2 3]) gives [1 3 6]. Similarly, cumprod gives cumulative product. diff is equivalent to a delta function in the sense that for a vector x of size N, diff(x, 1) gives [x(2) - x(1); x(3) - x(2);...x(N) - x(N - 1)], i.e. a vector of size 1 less and entires as first differences. For second order differences, use diff(x, 2) etc. etc. For more, see help diff. 

16. ceil, floor, round, and fix: ceil(x) rounds off to the integer greater than or equal to x; floor(x) rounds off to the integer less than or equal to x; round(x) rounds off the element of x to the nearest integer. If the fractional part of x >= 0.5, round(x) = ceil(x), otherwise round(x) = floor(x). Finally, fix(x) rounds the elements of x to nearest integers towards zero. So, fix works like ceil for x  <  0 and like floor for x  >  0.

17. This may be obvious, but doesn't hurt. If x is a matrix, then the operation y = 2 * x - 1 results in a matrix with each element of x operated upon similarly (a better way than using a for loop).

18. And finally (well, for now at any rate): There is another use of the so-very-useful colon ( : ) operator.  To convert a matrix to a vector, use:

>> x = x(:);

% this results in columns stacked one below the other (starting from column 1). As you would notice, a similar feat can be achieved by the use of the command reshape.

Am sure there are at least a few more functions or commands that I am not able to think of right now...though my feeling is that we have acquired most of the basic language that we might need to talk about our kind of problems (when I started out I didn't knew half the functions / commands that we have discussed so far; so my guess is we should do fine with what we have so far). I can't say this enough, but the more questions you ask, the better off both of us would be. In fact, the more out-of-the-box the better (however seemingly uncomfortable). 

Next Up: Control Flow / Loops, Importing Data / Files and Graphics

No comments: