Go to the first, previous, next, last section, table of contents.


2 Basics

Q2.1: Why does MATLAB only calculate to 4 significant digits?

It doesn't. It uses full double-precision floating point numbers to calculate everything. By default it only prints a few decimal places to the screen. You can change this using the command format long. Type help format for more information.

Q2.2: Why does the transpose operator take the complex conjugate?

When performing linear algebra operations on complex matrices, it is almost always the complex conjugate transpose (also called the Hermitian transpose) that is needed (see Gilbert Strang's linear algebra book for discussion- page 293 in edition 3). The bare apostrophe is an operator that takes the complex conjugate transpose. The non-conjugating transpose operator is a period followed by an apostrophe. Type help punct for more info.

>> A'     % complex conjugate transpose
>> A.'    % transpose

Q2.3: How can I set all NaN's to 0?

NaN's are tricky. NaN==NaN is false, so you can't say x(x==NaN) = 0; Instead, use the function isnan, as in: x(isnan(x)) = 0;

Q2.4: How can I make MATLAB use the full window width for displaying matrices?

Bob Gilmore writes:

In R12 (MATLAB 6.0), this can be controlled via a preference. Select the File | Preferences... menu item, and select Command Window in the Preferences dialog that appears. In the Display section, there's a checkbox labeled Limit matrix display width to eighty columns. Unchecking that box allows matrix displays to make full use of the Command Window's width. [Unchecked is the default.]

Starting with MATLAB R12.1, users can access the current command window size using the root property CommandWindowSize. That is, sz=get(0, 'CommandWindowSize'). In R12.0, there is no way to do this unless you call undocumented C functions from a MEX file.

Q2.5: How do I comment out a large block of code?

The built-in editor in MATLAB 6.0 now has a block-comment feature. Or you can use matlab-mode for Emacs, which supports this as well.

If you are using an older version, use this:

if 0
   commented out code
end

This is not the best solution, since parse errors inside that block will cause an error. But it's better than sticking comment characters in from of every line.

Q2.6: How do I save default settings across sessions?

The key is to create a STARTUP.M file. Look at the online help for more detailed instructions specific to your operating system.

Q2.7: How can I find local maxima in a vector array?

You can use the following one-line function to determine the indices of the local maxima.

function index = localmax(x)
index = find( diff( sign( diff([0; x(:); 0]) ) ) < 0 );

Q2.8: Why is 6*i not complex in my program?

You might have overwritten the variable i with a number earlier in your program (or session, for that matter). You can reset i using i=sqrt(-1), or "clear i", or you can use j, or you can use the function "complex".

Steve Lord tells me that an even better solution is to use 6i, which is unambiguously 6*sqrt(-1). In MATLAB versions >= 6.5, this can change a function which does not accelerate to one which does. For more details read http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/ch7_per7.shtml.

Q2.9: How can I modify the MATLAB path?

Easiest solution: use the PATHTOOL GUI. Or if you want command line access:

Suggested by Joshua Stiff: You can use addpath to add directories from the command line, and path2rc to write the current path back to `pathdef.m'. If you do not have write permissions for `pathdef.m', path2rc can write to a different file, which you can execute from your `startup.m'.


Go to the first, previous, next, last section, table of contents.