Hints
Hints
Hints
Matrices in MATLAB
The statements
u = [3; 1; 4]
v = [2 0 —1]
s=7
A = [1 2 3; 4 5 6; 7 8 10]
produce a column vector, a row vector, a scalar and a matrix.
u=
3
1
4
v=
2 0 —1
s=
7
A=
123
456
7 8 10
Colon :
The colon is one of the most useful operators in MATLAB. It can create vectors, subscript arrays, and
specify for iterations. The colon operator uses the following rules to create regularly spaced vectors:
where i,j, and k are all scalars.
Below are the definitions that govern the use of the colon to pick out selected rows, columns, and
elements of vectors, matrices, and higher-dimensional arrays:
j:k is the same as [j,j+1,...,k]
j:k is empty if j > k
j:i:k is the same as [j,j+i,j+2i, ...,k]
j:i:k is empty if i > 0 and j > k or if i < 0 and j < k
A(:,j) is the j-th column of A
A(i,:) is the i-th row of A
A(:,:) is the equivalent two-dimensional array.
For matrices this is the same as A.
A(j:k) is A(j), A(j+1),...,A(k)
A(:,j:k) is A(:,j), A(:,j+1),...,A(:,k)
A(:,:,k) is the kth page of three-dimensional array A.
A(i,j,k,:) is a vector in four-dimensional array A. The vector includes
A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.
A(:) is all the elements of A, regarded as a single column. On the left side of an assignment
statement, A(:) fills A, preserving its shape from before. In this case, the right side must contain the
same number of elements as A.
Plots, Graphic
On the PC, you can import a MATLAB graphic into another application by copying the Figure to the
clipboard in Windows Bitmap or Windows Enhanced Metafile format, and then pasting the graphic
into the other application.
There are two ways to copy a Figure to the clipboard:
• Select the Copy Figure command from the Edit menu of the Figure window. The format of the
output is determined by preferences you can set.
• At the command line, use the print command with the –dbitmap or –dmeta option. Do not provide
a filename. The Figure will be copied to the clipboard as a Windows Bitmap or Metafile, depending
on which switch you use. You then import the graphic into another application by using the Paste
command.
Choosing the Format. The Windows Bitmap and Enhanced Metafile formats are fundamentally
different in the way they represent the Figure. The Bitmap format creates a bitmapped copy of the
Figure window, while the Metafile format uses a vectorized approach. In general, the bitmap format is
of lower resolution than the Metafile format.
The Windows Enhanced Metafile format is a device-independent format for sharing graphics between
Windows applications. This format is capable of producing high-quality graphics, and is the preferred
graphics format to use on Windows systems.
Application-Specific Issues
• Microsoft Word. When you import a graphic into a Microsoft Word document, first create a frame
in the document and import the graphic into it. Importing into a frame will enable you to reposition
the graphic by dragging it.
• Corel Draw. You can import Windows Enhanced Metafiles and PCX files into Corel Draw. Note
that the graphic appears to be black and white until you make the picture full screen.
• LaTeX. You can import a MATLAB Figure into LaTeX by creating an Encapsulated PostScript file.
The general syntax for including a PostScript figure in LaTeX is:
\begin{figure}[h]
\centerline{\psfig{figure=file.ps,height=height,angle=angle}
\caption{caption}
\end{figure}
You can specify the height in any LaTeX compatible dimension. To set the height to 3.5 inches, use
the command:
height=3.5in
You can use the angle command to rotate the graph. For instance, to rotate the graph 90 degrees,
use the command:
angle=90