>> 2 + 2
ans = 4
>> ans
ans = 4
The answer variable is important since you can suppress the output of the result of any calculation by appending a semicolon to the command.
>> 1 + 2;
The result is still stored in the ANS variable. "ANS;", of course, will not do anything.
>> ans
ans = 3
>> ans;
The second most important command is probably the help command. What the manual ("man") pages are for UNIX is the HELP command for Matlab. The program consists of thousands of commands, I counted more than 5000 on my installation, and you can not (and probably want not) to remember them all. If you are looking for a command you can use the LOOKFOR command following by a word or phrase you are searching.
>> lookfor average
MEAN Average or mean value.
If you know (or found out) the name of a function the HELP function will tell you what it does and how you can use it.
>> help mean
MEAN Average or mean value.
For vectors, MEAN(X) is the mean value of the elements in X. For
matrices, MEAN(X) is a row vector containing the mean value of
each column. For N-D arrays, MEAN(X) is the mean value of the
elements along the first non-singleton dimension of X.
[...]
If the HELP comment does not help you, you can read the source code of most functions with the TYPE command. Although the function that come with the program are usually well documented, some academic (or your own) toolboxes are not. When you start writing your own functions "TYPE MEAN" becomes handy, as it provides a short overview how to define a function (look at the first line) and how you write the help text (second line and thereafter).
>> type mean
function y = mean(x,dim)
%MEAN Average or mean value.
% For vectors, MEAN(X) is the mean value of the elements in X. For
[...]
y = sum(x)/size(x,dim);
else
y = sum(x,dim)/size(x,dim);
end
The major advantage of the Matlab language (and similar languages) is the possibility to apply arithmetic operations to arbitrary large data sets with the same simplicity as calculating 2+2. Vectors are defined by squared brackets. To get a row vector just type the single coordinates side by side.
>> [1 2 4] + [1 2 4]
ans = [2 4 8]
But for larger vectors it would not make sense to print its values on the command line. The more interesting option is to use a ";" at the end of the calculation and one of the many plot commands.
>> [1 2 4] + [1 2 4];
>> plot(ans);
and
>> plot(ans, '.');
Your can try other operations, "HELP +" will print a list. What does "[ans ans]" do?
>> whos
Name Size Bytes Class
ans 1x3 24 double array
Grand total is 3 elements using 24 bytes
"1x3" means one row (hence a row vector) and 3 columns. To create a column vector you can either use ";" or the transpose operator "'".
>> [1;2;3]
[...]
>> whos
[...]
>> [1 2 3]'
[...]
>> whos
[...]
What does [1,2,3] do? What [ans;ans] or [ans ans]? If you are familiar with complex number try to find out the difference between the ' and .' operator. (Note: type i or e.g. 1+2i for a complex number)
Page Execution took 7.544 seconds |