9/6 1996, by andersh@maths.lth.se. HTML-verson at http://www.maths.lth.se/matstat/andersh/matlabfaq.html
This was a FAQ once maintained by mwette@grover.jpl.nasa.gov (Matt Wette) and since a couple of years by andersh@maths.lth.se (Anders Holtsberg), and from now maintained by NOBODY since I've moved to a new job! Anybody may pick up this and continue!
PROGRAMS AND BOOKS Are there any Matlab programs that do XYZ? What books and papers are there?
MATLAB USER QUESTIONS Is there any automatic Matlab to C converter? When will Matlab for Linux arrive? How can I do a contour plot with unevenly spaced data? What about undocumented functions in Matlab? What is this `system_dependent' function? How do I fix printing on A4-paper? Why is OS/2 Warp's Win and Matlab 4.2 crashing? Why is the histogram function so slow?
QUICK LITTLE MATLAB HACKS Why is my m-file so slow? How can I initialize a this low rank matrix easily? How can I load-from or save-to a file whose name is in a string variable? How can I store the result of `!ls *.dat' in a variable? How can I make a variable global from within a function? What is a fast way to compute a moving average? How many times does "a" occur in "x"? How can a large program block be commented out? What's the area of a polygon? How can I set all NaN's to 0? What's the inverse mapping of the sort order? How do I access the diagonal without a for-loop? How can I vectorize Gray-code conversion? How can I vectorize distance computations?
The newsgroup 'comp.soft-sys.matlab' is a forum for discussing issues related to the use of Matlab, the scientific calculation and visualization package from MathWorks Inc. This includes discussion of similar software packages like Xmath, Octave, RLab. Appropriate discussion in the group will include both general Matlab issues and platform-specific questions, and discussion comparing Matlab to other systems.
[2] Where can I find information on Matlab and Simulink?
Lots of information can be found at The Mathworks,
http://www.mathworks.com/faq.html ftp.mathworks.com
[3] Are there many Matlab-type environments?
Yes. Matlab competes in one way or another with the packages listed below. Since these programs are not oriented mainly towards symbolic manipulation we have left out Matematica, Maple, Derive and the likes. See sci.math.symbolic and comp.soft-sys.math.mathematica for discusson groups dedicated to those programs. Note though that Matlab may be sold together with a Maple kernel, so that one may do symbolic calculations from whithin Matlab.
==== First we have the commercial ones in alphabetical order. ====
246 Church Street, Suite 307
New Haven, CT 06510
Tel: 203-777-7442
Fax: 203-776-4074
???
???
???
Cochituate Place
24 Prime Park Way
Natick, MA 01760
Tel: 508-653-1415
Fax: 508-653-2997.
Email: info@mathworks.com
Harmonic Software, Inc
12223 Dayton Avenue North
Seattle, WA 98133
Tel: 206-367-8742
Fax: 206-367-1067
Email: harmonic@world.std.com
sunsite.unc.edu://pub/Linux/apps/pv-wave
1700 Westlake Ave. N.
Suite 500
Seattle, WA 98109
Tel: 206-283-8802
Fax: 206-283-6310
There is a very active user group (of mainly statisticians)
with a mailing list that can be found in the FAQ.
FAQ: http://www.stat.math.ethz.ch/~roosen/S-faq_toc.html
ftp.stat.math.ethz.ch in /pub/Doc as S-faq.*
see also ftp://netlib.att.com/netlib/att/stat/info/proj.html
3260 Jay St.
Santa Clara, CA 95054-3309
Tel: (408) 980-1500
Fax: (408) 980-0400
Email: xmath-info@isi.com
WWW: http://www.isi.com
==== Then we have the non-commercial ones in alphabetical order. ====
Check out the following page too, it is probably more complete on this point.
http://www-ocean.tamu.edu/~baum/graphics-analysis.html
M.Wette, "Caesy: A Computer-Aided Engineering System" Proc. 1992 Symp. on Computer-Aided Control System Design Napa, CA, March 1992
draci.cs.uow.edu.au in /netlib/matcalc/ csi.jpl.nasa.gov in /pub/matlab/matcalc/
M. Koga and K. Furuta, "MaTX: A High-Performance Programming Language (Interpreter and Compiler) for Scientific and Engineering Computation," Proc. 1992 Symp. on Computer-Aided Control System Design, Napa, CA, March 1992
excel2.uwaterloo.ca [129.97.86.13] in /pub/
See also
Pang, G.K.H.,"Knowledge-based Control System Design", in Recent Advances in Computer-Aided Control Systems Engineering, Jamshidi, M and Herget, C.J. (ed.), Elsevier Science Publishers, 1992.Pang, G.K.H., "A Knowledge Environment for an Interactive Control System Design Package", Automatica, Vol. 28. No. 3, pp. 473-491, May 1992.
ftp.cica.indiana.edu in win3/programr.
http://orion.animal.uiuc.edu/
http://www.che.wisc.edu/octave
ftp.che.utexas.edu in /pub/octave/
Linux binaries also at ftp.lightning.eee.strath.ac.uk
in /pub/programs/../users/duncan/octave
mailing list for Octave:
help-octave-request@che.utexas.edu
help-octave@che.utexas.edu
Sources:
evans.ee.adfa.oz.au in pub/RlaB
csi.jpl.nasa.gov in pub/matlab/RLaB
Binaries:
Unix: evans.ee.adfa.oz.au in pub/RLaB/Target-*
Usually SunOS-4.x, Linux, and BSD-386.
sunsite.unc.edu for the Linux version also.
DOS: ftp://oak.oakland.edu/SimTel/msdos/math/rlab117d.zip
evans.ee.adfa.oz.au in area pub/RLaB/Target-DOS
MAC: ftp.stat.ucla.edu//apps/mac/rlab/rlab-117dx.hqx (2964K)
info-mac//sci/rlab-117dx.hqx (2964K)
ftp.inria.fr://INRIA/Projects/Meta2/Scilab/
ftp://tsx-11.mit.edu/pub/linux/...
Author Pekka.Janhunen@fmi.fi Distribution site ftp.funet.fi:pub/sci/math/tela Tela bugs tela-bugs@fmi.fi Tela suggestions tela-suggestions@fmi.fi Tela WWW-page http://sumppu.fmi.fi/prog/tela.html
wuarchive.wustl.edu: /edu/math/msdos/calculus/cc4-9206.zip
UNIX: wuarchive.wustl.edu: /languages/yorick/yorick-1.0.tar.gz sunsite.unc.edu: /pub/languages/yorick/yorick-1.0.tar.gz sunsite.unc.edu: /pub/Linux/apps/math/matrix/yorick-1.0.tar.gz etlib.att.com: /netlib/env/yorick-1.0.tar.gz netlib2.cs.utk.edu: /env/yorick-1.0.tar.gz MAC: sumex-aim.stanford.edu: /info-mac/dev/yorick-* and its many mirrors
==== Properties of some of the free programs. ====
The following is compiled from the net.
RLaB Octave SciLab Tela
DOS version + - - -
documentation + + - ?
fast + - ? +
Matlab compatible - + - converter
The plotting on all of them is not as good as with a commercial package.
They either use Gnuplot or have some other builtin graphics. from sundaram@news.cig.mot.com (Divya A. Sundaram) ... I am running Octave and SciLab under Linux (and also have matlab on the Windows box and on the Macintosh). I find Octave to be quite suitable for MOST stuff I used to do under Matlab (and find that most commands and syntax are the same or VERY similar). SciLab is something else. It is absolutely amazing and I have decided to spend some time learning it more thoroughly. I feel (personal opinion only) that it is a FAR more complete system than Octave or Matlab 4.0.
==== A proposed standard ====
There is an IFAC/IEEE-CSS Working Group developing standards and guidelines for these tools. A proposed standard command language was developed, is described in the document "Command Language Standard for CACSD Software" and is available from
Magnus Rimvall
Control Systems Laboratory, GE-CRD
Schenectady, NY 12301
[4] Are there any Matlab programs that do XYZ?
A nice function is LOOKFOR, with the help of which you can search for a Matlab function with a keyword on your own computer. If there isn't there might be in one of The Mathworks' toolboxes, if you don't have them all. The Mathworks' toolboxes are the following, on which you can find more information at The Mathworks site.
For graphics check out a web page that lists and provides descriptions and links to many freely available data analysis and graphic pages on the net:
http://www-ocean.tamu.edu/~baum/ocean_graphics.html
Some major interesting matlab tools are the following.
ftp.apple.com:/pub/malcolm
Scientific Software Group 72-78 Grand Rue 92310 Sèvres (France) Phone: (33) 1 45 34 23 91 Fax: (33) 1 45 07 08 06 E-mail: info@ssg.fr"At the Division of Structural Mechanics and Solid Mechanics in Lund we have developed a finite element software as a toolbox to Matlab. The program is called CALFEM. It is used both in teaching and research at the departments."
http://hult.byggmek.lth.se/Calfem/calfem.html
ftp.mathworks.com://pub/contrib/optim/genetic http://www.flextool.com
ftp://ftp.mathworks.com/pub/contrib/graphics/guimaker/
/pub/contrib/misc/fuzzy_inference_systems.sh /pub/contrib/misc/pc_fuzzy_inference_systems.shSee also a commercial package at
http://www.flextool.com
Jyh-Shing Roger Jang
[5] What books and papers are there?
At The Mathworks site there are m-files that are used in the
following books. The authors and full titles et c are also at
The Mathworks site.
There are at least some Matlab introductions on the net;
[6] Is there any automatic Matlab to C converter?
Mathworks is now selling its translator. Another alternative is
MATCOM, which is a is freeware translator
of MATLAB to C++. It seems to work pretty
well, but can be confused by feval and gives up on eval.
C++ compiler (Borland C++/Visual C++/DJGPP) on a PC compatible is
required, or a UNIX system.
For Borland C++/Visual C++ Windows is needed.
[7] When will Matlab for Linux arrive?
Matlab 4.2c for Linux is released and selling!
Well, use GRIDDATA. However, since this is unlikely to work read
what Kirill Pankratov says:
[9] What about undocumented functions in Matlab?
from Cleve Moler ...
Almost everything in the "demos" directory is not described in the
User's Guide. There are lots of goodies there. In 4.0, the demos are
best place to see examples of Handle Graphics.
There are other undocumented functions in directories other than demos.
Some of them are "worker" functions that are unlikely to be used directly;
they are simply called by other functions. A few, like COMET and COMET3,
were actually written after the User's Guide had been sent to the printer.
[10] What is this `system_dependent' function?
from John Little (w/editing) ...
system_dependent(2,0) turns off command-line editing.
system_dependent(2,1) turns it back on.
system_dependent(3) is unused.
system_dependent(4,i,j) is used to program the command-line
editing keys. See CEDIT.M for more
information.
system_dependent(5) enables core dumps in the event of seg
faults. Normally MATLAB tries to catch
seg faults and "continue".
From Andreas Payer
(payer@cipserv1.physik.uni-ulm.de):
The problem is, that MATLAB resets the printer driver back to its
'DefaultFigureSize' after each printing. My solution is a file 'startup.m' in
my MATLAB home-directory with the following contents:
From Chritian Schmid
(cs@esr.ruhr-uni-bochum.de): ...I had the same problem and solved
it by adding the following statement:
set(0,'DefaultFigurePaperType','A4');
to my matlabrc.m file.
I found also, that you can retrieve all default setting keywords
by entering getpref (undocumented, and no help available) and adding
the prefix 'Default' to the keyword in the set(0,...)-statement.
There is a bug in the vwin.sys file
in Warp's \os2\mdos directory.
The updated file from IBM can be ftp'd from
ftp.mathworks.com in /pub/mathworks/bin/os2warp
This is a tricky issue. A discussion including improved code
can be found in
The first and main trick is "vectorizing", i e
avoiding for-loops. Do NOT write things like
If you really must have for-loops then preallocate memory,
it may be a large gain for large vectors, like in the following
example.
Another time-saver is using sparse matrices if that is possible.
As a last resort one may rewrite the innermost loops
into cmex-files.
[15] How can I initialize a this low rank matrix easily?
Here's "Tony's trick", attributed to Tony Booer of Schlumberger:
To initialize a matrix with values "a" in the first row and values "b" in
the second row, do the following.
with the variant
In Matlab 3.x you have to do something like
[17] How can I store the result of `!ls *.dat' in a variable?
[18] How can I make a variable global from within a function?
You can in Matlab 4. The following hack is from
Laura Needles
Define the script file `mkglobal_1.m' as
Define the script file `mkglobal_2.m' as
[19] What is a fast way to compute a moving average?
A fast way of computing a moving average of "m" consecutive
numbers in the row vector "x" of length "n" is
[20] How many times does "a" occur in "x"?
Mauro Jorge Atalla asked if there was a clever vectorized way of
counting how many times the components of a matrix are
repeated in the matrix and wanted the result to
return a matrix with rows of the form
Use Tom L. Davis' trick.
[22] What's the area of a polygon?
If "x" and "y" are the coordinate points of a polygon
(supposing the last coordinate
is the same as the first one), then the shortest and fastest solution
seen on the net so far
to the problem of computing the area is the following one.
[23] How can I set all NaN's to 0?
If a matrix "x" contains NaN's they can be set to 0 by
for example Jeffery Faneuff's solution
The following works for matrices that are sorted columnwise;
Use Loren Shure's trick for a n x n matrix "A",
The question came up how to vectorize the following code.
Here is a tip from Frank Reine (reine@csd.de) for speeding up the distance
calculations in for example clustering algorithms.
Consider a NxK pattern matrix p with the patterns in its
rows and a MxK centroid matrix w with cluster centers in the rows. Our
goal is to calculate the NxM distance matrix d. Now, instead of using
something like
csi.jpl.nasa.gov in /pub/matlab/contrib/
http://pc5.math.umbc.edu/~yzhang/lipsol/
ftp.math.umbc.edu:pub/zhang/lipsol/v0.3/
kerog@sp.isl.secom.co.jp (Keith Rogers)
http://www.eeap.ogi.edu/~ericwan/fir.html
http://www.iau.dtu.dk/Projects/proj/nnhtml.html
ftp.std.com in /vendors/metamat
gopher.std.com under "Shops on The World"
Eigenvector Technologies
4154 Laurel Dr.
West Richland, WA 99352
http://www.vvv.com/~caughey/ahlea
http://crab.sp.ph.ic.ac.uk/qplot/
ftp://crab.sp.ph.ic.ac.uk/pub/qplotdoc.tar.gz
Free demo version at novell.felk.cvut.cz (192.108.154.33)
e-mail: houska@control.felk.cvut.cz
ftp.nps.navy.mil /pub/ece in the spctools directory
Rob Lake (rbl@hal.cwru.edu) said "... Lots of documentation.
The only problem is that the .mex functions provided
are for a Sun. As I recall, I e-chatted with Lt. Brown a few years ago
and the problems with the Mac C compilers made porting the .mex files
a bit challenging. ... If and when I get the whole kit converted,
I'll post ..."
http://www.alumni.caltech.edu/~ingber/index.html#ASA-CODE
ftp://ftp.apple.com/pub/malcolm/SoundAndImageToolbox.cpt.hqx
http://puddle.mit.edu/~glenn/kirill/saga.html
http://www.maths.lth.se/matstat/datalib
ftp.maths.lth.se in /pub/transfer/andersh
ftp://ftp.mathworks.com/pub/contrib/graphics/stextfun/
ftp://ftp.mathworks.com/pub/contrib/graphics/stextfun/
Another one is LINK, an
application to simulate multi-font, multi-object
text strings in MATLAB. Strings are entered in
a LaTeX-like syntax and link formats and positions
the result. This toolkit is also at
ftp://ftp.mathworks.com/pub/contrib/graphics/
simplicity.stanford.edu:/pub/taswell
control.cheg.nd.edu:/pub/Kantor/matlab/wavetools/wavets
cml.rice.edu:/pub/dsp/{software,papers}
ftp.tsc.uvigo.es:/pub/Uvi_Wave/matlab/
http://www.mat.sbg.ac.at/~uhl/wav.html
http://jazz.rice.edu/software.html
info.mcs.anl.gov in pub/W-transform
Computer-Based Exercises for Signal Processing Using MATLAB
Digital Control and Estimation: A Unified Approach
Numerical Methods for Physics Using MATLAB
Numerical Methods for Mathematics, Science and Engineering
Introduction to Linear Algebra
Templates for the Solution of Linear Systems ...
Digitale Signalverarbeitung ...
ftp.math.ufl.edu/pub/matlab
primer35.tex at math.ufl.edu in /pub/matlab
math.berkeley.edu:/pub/Preprints/Jim_Demmel/Ma228a/MatlabPrimer4_0_1.tex
math.berkeley.edu:/pub/Preprints/John_Strain/228a/MatlabPrimer4_0_1.tex
ElementsOfMatlab.ps.Z at cs.colorado.edu in /pub/HPSC
ftp://csi.jpl.nasa.gov/pub/matlab/matlab-books
Home Page (now) -
http://techunix.technion.ac.il/~yak/matcom.html
Mirrors (in a few days) -
http://rio.esm.vt.edu/mirror/matcom
ftp://ftp.eeng.dcu.ie/pub/matlab/MATCOM
ftp://ftp.funet.fi/pub/sci/math/matlab
File name is matcom.zip, file size is 1,154,212 bytes.
To unzip it use PKZip 2 or compatible.
sunsite.unc.edu://pub/Linux/apps/math/matrix
sunsite.unc.edu://pub/Linux/apps/pv-wave
[8] How can I do a contour plot with unevenly spaced data?
set(0,'HideUndocumented','no')
system_dependent(1) deals with the MATLAB-ACSL interface.
ACSL is a simulation language that
MATLAB can talk to. See ACSL.M for
more information.
set(0,'DefaultFigurePaperType','a4letter',...
'DefaultFigurePaperOrientation','landscape',...
'DefaultFigurePaperPosition',[0.5 0.5 11.1 7.7]);
You can play a little bit with the settings of the
'DefaultFigurePaperPosition' if you like, but with these settings it is
guaranteed that the plot labels appear on the paper.
http://www.maths.lth.se/matstat/andersh/histogr
QUICK LITTLE MATLAB HACKS
[14] Why is my m-file so slow?
for i = 1:n % 3 BAD examples
z(i) = x(i) * y(i);
end
for i = 1:n
for j = 1:m
if A(i,j) > B(i,j)
C(i,j) = D(i,j);
else
C(i,j) = F(i,j);
end
end
end
for i = 1:n-2
Z(i) = 4*X(i+2) + 2*X(i+1) + X(i);
end
but write instead
z = x .* y; % The GOOD versions
J = A > B;
C = D .* J + F .* (~J);
W = [4 2 1];
Z = filter(W,1,X);
Z = Z(3:n);
Apart from looking much nicer it is incredably much faster for
large vectors and matrices. Vectorizing complicated
code is an interesting sport as you can see further down
in this FAQ.
x = zeros(10000,1); % Preallocation of memory!
x(1) = rand;
for i = 2:10000
x(i) = ( 1 - 1.73*x(i-1))^2;
end
Tested on a sparc machine this runs on 2.6 seconds with the first line and
79.3 seconds without it!
[a(1,ones(1,m)) ; b(1,ones(1,m))]
[a; b] * ones(1,m)
[16] How can I load-from or
save-to a file whose name is in a string variable?
name = 'xyz.mat';
eval(['save ', name]);
eval(['load ', name]);
In Matlab 4.x the first of the following lines is
actually interpreted to mean exactly the same as the
second one;
myfunction qwerty
myfunction('qwerty')
which means that "load(name)" may be used in Matlab 4.x.
Now you figure out why the following statement gives a result that
is intuitively weird at the first sight;
sin pi
[stat, list] = unix('ls *.dat');
The variable `stat' contains the return status, the variable `list'
contains the output.
function [] = mkglobal(var)
% MKGLOBAL mkglobal(var) - make var a global variable
if ~exist('mkglobal_var'), mkglobal_1; end;
mkglobal_var = var; mkglobal_2
global mkglobal_var
eval(['global ', mkglobal_var]);
z = [0 cumsum(x)];
y = ( z(m+1:n+1) - z(1:n-m+1) ) / m;
[number, number_of_times_it_appears_in_the_matrix]
A solution from Erik A. Johnson is
sortM = sort(M(:));
uniqueM = [diff(sortM);1] > 0;
count = [sortM(uniqueM) diff(find([1;uniqueM]))];
Furthermore, if the elements are known to be integers
then Hans Olsson's one-liner can be used:
full(sum(sparse(1:length(M(:)),M(:),1)))
[21] How can a large program block be commented out?
if 0,
commented out code
end
That works however only if the parser doesn't complain about the
commented out code. If that is the case you can ask
kirill@slough.mit.edu (Kirill Pankratov) of his BLOCKCOM function.
area = abs(sum(diff(x).*(y(1:nl-1)+y(2:nl)))/2);
x(isnan(x)) = zeros(size(x(isnan(x))));
Unfortunately
x(isnan(x)) = 0; %ERROR
does not work. The Mathworks says it will work in the next release.
By the way, figure out for yourself
why the following program gives its interesting
result!
x = 1:5
for i = 1:10, disp(x(ones(1,i))), end
[24] What's the inverse mapping of the sort order?
[xs, i] = sort(x);
[dummy, j] = sort(i);
However, if "x" is an ordinary vector of length "n" a nice
alternative is
[xs, i] = sort(x);
j = 1:n;
j(i) = j;
[25] How do I access the diagonal without a for-loop?
J = 1:n+1:n^2;
D = A(J); % Another form of "D = diag(A);".
A(J) = zeros(1,n); % This is not possible to do with "diag".
[26] How can I vectorize Gray-code conversion?
% convert from Gray code to binary code
B = zeros(1,n);
B(1) = G(1);
for i=2:n
B(i) = xor(B(i-1),G(i));
end
To this sjs@mod.dsto.gov.au (Stephen Searle) answered
B = rem(cumsum(G),2);
[27] How can I vectorize distance computations?
for i = 1:M
d(:,i) = sum((p-ones(N,1)*w(i,:))'.^2)'
end
as it is done e.g. in the fuzzy toolbox, try using
s2 = sum(p'.^2)';
w2 = sum(w'.^2);
d = s2(:,ones(1,M)) - 2*p*w'+ w2(ones(N,1),:);
as a fully vectorized version. There is a significant improvement in
execution speed, especially for large K.