更多请见:http://bbs.dartmouth.edu/~fangq/MATH/FAQ/
20).Matlab中能开的最大数组是由什么决定的?
:# chenft (mike),2002/6/1, SMTH/MathTools #
I have had similar problems. Below is an explanation I received from
Ian Boyd
from Mathworks (just giving credit where credit is due) that explains
what's happening. You solution is to run matlab with the -nojvm mode.
"The heap memory system in JAVA consists of data and handle elements.
When you allocate a variable you get a handle and data. As long as
data has an associated handle, the JVM considers it valid and
will not clean it up.
However, when you call the clear function in MATLAB, all handles are
destroyed, and the data associated is now invalid. This means that
the JAVA engine can free up that data (garbage collection), but does
not mean that it will clean it up at that moment.
Calling the PACK command encourages JAVA to run the garbage collector
and de-fragment the memory. But it does not force it to (This is part
of the JAVA design). Even though the memory is 'freed' on the heap,
it is not actually free to the OS, it is only free to the JVM. Here
is one way to think of it:
[MATLAB]
[JAVA]
[OS]
MATLAB runs on JAVA (virtual machine), and Java runs on the OS
(physical machine). So when MATLAB is running in JAVA mode memory
allocations are requested from the JRE, not the OS.
One problem you may be running into is that the default maximum
JAVA heap size is relatively low ( <= 64 MB), so that is all the
memory one session of MATLAB will ever get on your system.
The good news is that you can increase this value. You will need
to create a java.opts file in $MATLAB/bin/$ARCH (or in the current
directory when you start MATLAB) and put the following command:
%%%BEGIN CODE%%%
maxHeapSize = 268435456
%%%END CODE%%%
This will give you 256MB of JVM memory and you can adjust the
parameter as needed.
Note: $MATLAB is the root directory and $ARCH is your system
architecture. This solution works on Windows as well as Solaris,
Linux,Alpha, and SGI. A similar operation is possible on IBM and
HPUX, but with a different syntax.
For the 1.1.8 JVM (Windows, Linux, Solaris, Alpha, SGI) our
defaults are:
minHeapSize = 16000000
maxHeapSize = 64000000
These are the structure field names in that correspond to
-ms and -mx, and the settings above are roughly 16MB and 64MB.
To investigate the Java heap a bit, ask via the following:
>> java.lang.Runtime.getRuntime.totalMemory
>> java.lang.Runtime.getRuntime.freeMemory
When the free memory hits zero, Java will double the heap size
(up to the maximum setting).
If you choose to run without Java, you will remove the overhead
of the middle man, but you will also lose some MATLAB functionality
(mostly graphics and the Editor). You will still have most
of the computational power though.
Without JAVA, memory management will come directly from the OS,
and a CLEAR operation will result in memory being freed back to
the OS.
=================================== - [返回]
21).如何在Matlab中添加新的工具箱?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
如果是Matlab安装光盘上的工具箱,重新执行安装程序,选中即可。
如果是单独下载的工具箱,一般情况下仅需要把新的工具箱解压到某
个目录,然后用addpath(对于多个目录的使用genpath()或者pathtool添
加工具箱的路径,然后用which newtoolbox_command.m来检验是否可
以访问。如果能够显示新设置的路径,则表明该工具箱可以使用了。
具体请看工具箱自己代的README文件。
=================================== - [返回]
22))如何读写Matlab的.mat文件?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
文件结构参见:
http://www.mathworks.de/access/helpdesk/help/pdf_doc/matlab/
matfile_format.pdf
http://www.mathworks.com/support/solutions/data/8757.shtml
ftp://ftp.mathworks.com/pub/tech-support/solutions/s8757/
readmemat.txt
建议使用matlab自己提供的函数来读写简单安全,或者参考:
http://engineering.dartmouth.edu/~fangq/MATH/download/
source/mat_file.txt
来自matlab的c math library
=================================== - [返回]
23).如何得到contour线上的坐标点?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
lcount=5;
[c,h]=contour(peaks,lcount);
x=get(h,'xdata');
y=get(h,'ydata');
这里得到的x和y都是cell数组,用x{1}/y{1}来得到每条线上的坐标对,
注意,每条线的最后一个数据是NaN
=================================== - [返回]
24).如何将Matlab绘制的三维网格图帖到word里?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
如果需要位图,好处是所见即所得,坏处是图像精度差,不能放缩:
1.用拷屏 Alt+PrintScreen
2.在图形窗口菜单Edit\Copy Options….\选择Bitmap,可以
选择透明背景,然后Edit\Copy Figure
如果需要拷贝矢量图:
在图形窗口菜单Edit\Copy Options….\选择Metafile,然后
Edit\Copy Figure,在Word中粘贴
经常地,按照Metafile方式粘贴的图片曲线会出现锯齿,最好的方式是
使用eps文件:
1.将需要拷贝的图作为当前窗口
2.再转换到matlab命令窗口,print -deps filename.eps
3.-deps还可以用depsc,deps2,depsc2
4.在word中插入图片,选中该eps,如果是word 2000以前版本
,不会显示图片内容,但可以打印,word XP即可显示,又可打印。
5.如果不满意,可以在word中双击编辑,如果安装有Adobe
Illustrator等矢量图像编辑软件,也可以进行编辑。
=================================== - [返回]
25).请问可以查看Matlab中函数的源代码吗?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
Matlab除了buildin函数和mex/dll文件看不到原码,其他如工具箱等都可
以直接看到代码,首先确认该文件安装在matlab中,即which
filename.m存在,然后可以edit filename.m
=================================== - [返回]
26).Matlab有没有求矩阵行数/列数/维数的函数?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
ndims(A)返回A的维数
size(A)返回A各个维的最大元素个数
length(A)返回max(size(A))
[m,n]=size(A)如果A是二维数组,返回行数和列数
nnz(A)返回A中非0元素的个数
=================================== - [返回]
27).Matlab中如何中断运算?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
在命令窗口按Ctrl+C,在UNIX/LINUX会立即中断运算,在Windows可
能由于操作系统的原因,有时会出现死机和等待的情况。
=================================== - [返回]
28).Matlab中有没有画圆或椭圆的函数?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
没有,Matlab没有提供直接绘圆的图元函数,需要自己写代码,其实
就两句:
sita=0:pi/20:2*pi;
plot(r*cos(sita),r*sin(sita)); %半径为r的圆
plot(a*cos(sita+fi),b *sin(sita+fi)); %椭圆
如果是单位圆,可以使用rectangle('Curvature', [1 1])
=================================== - [返回]
29).Matlab下如何定义整形
:#修改:fhorse (马不停蹄),2002/6/21,SMTH/MathTools #
Matlab默认的矩阵数据结构都是双精度浮点型,即64位来表示一个数
字,大多数的函数和操作都定义在double数据结构,如果你需要
把double的数据转换为整形,然后再参与运算,需要使用
double(int32(x))或者floor/round/ceil等函数
如果为了节省内存,只进行赋值、打印等简单操作,可以参
见uint8/uint16/uint32命令的帮助
=================================== - [返回]
30).Matlab如何产生均匀分布的白噪声?
:#misc,2002/6/21,SMTH/MathTools #
help rand 均匀分布百噪声
help randn高斯分布百噪声
=================================== - [返回]
31).在Matlab中debug的时候能否跟踪变量的?
:#FangQ(Qianqian.Fang@dartmouth.edu),2002/6/21,BigGreen/MathTools #
可以,如果使用medit,设置断点后可以用鼠标移到所看的变量上,显
示当前的值,或者在命令窗口打该变量名直接回车。如果在代码中实
现调试断点等功能,参
见dbstop,dbcont,dbstep,dbclear,dbtype,dbstack,dbup,dbdown,dbstatus,
dbquit
=================================== - [返回]
32).请问在Matlab中怎样输入特殊符号啊或者上标、下标?
:#FangQ(Qianqian.Fang@dartmouth.edu),southerner(笑着),2002/6/6,SMTH/MathTools#
matlab的text/title/xlabel/ylabel对象支持简单的TeX排版语法,如希腊字
母,上下标等例如
text(0.5,0.5,'\alpha^\beta_2');
=================================== - [返回]
33).Matlab中如何后台运行一个DOS程序?
:#FangQ(Qianqian.Fang@dartmouth.edu), 2002/6/4. BigGreen/en_Matlab#
这里是一个后台执行一个需要外部输入的DOS命令的例子,需要的输
入实事先都写在同目录下的input.txt文件中:
dos('myexe < input.txt &')
=================================== - [返回]
34).Matlab如何加载输入文件(批处理模式) ?
:#翻译自:comp.sys-soft.Matlab FAQ. BigGreen/en_Matlab#
PC上可以使用matlab /r参数来在matlab启动的时候直接加载运行m文件
,在UNIX上,使用
matlab < MyMFile > MyOutputFile
来外部执行MyMFile,
以上执行方式都可以通过脚本文件实现批处理
=================================== - [返回]
35).Matlab如何启动时执行规定的文件?
:#FangQ(Qianqian.Fang@dartmouth.edu), 2002/5/29.BigGreen/en_Matlab#
参见上一个问题的回答
=================================== - [返回]
36).如何在Matlab GUI中使用图形背景?
:#FangQ(Qianqian.Fang@dartmouth.edu), 2002/5/29.BigGreen/en_Matlab#
这是一个简单的例子:
[A,map]=imread('yourimg.gif');
imagesc(A)
colormap(map)
set(gca,'position',[0 0 1 1])
axis off
ax2=axes('position',[0.2,0.2,0.6,0.6]);
plot(rand(1,10),'parent',ax2);
set(ax2,'color','none')
=================================== - [返回]
37).大量数据点Matlab绘图为什么很慢?
:#FangQ(Qianqian.Fang@dartmouth.edu), 2002/6/22.BigGreen/en_Matlab#
1.首先看能否用已有函数对整个矩阵绘图,比
如mesh/plot3/trimesh等
2.如果必须一点一点/或者一条线一条线的添加,最好作如下
设置:
doublebuffer=on
erasemode=none
backingstore=off
renderer=opengl
以及参考MathWorks对于高速绘图的tips:
http://www.mathworks.com/support/tech-notes/v5/1200/1203.shtml,
=================================== - [返回]
38).Matlab中如何求解广义积分?即积分限到有无穷的或者有奇异点的积分(瑕积分)?
:#FangQ(Qianqian.Fang@dartmouth.edu), 2002/6/22. BigGreen/en_Matlab#
Matlab的quad/quad8只能作定积分,广义积分需要自己来写程序逼近,
流程大概如下:
1.设定收敛限epsi
2.把为inf/-inf或者歧义点的积分限设置为一个初始值,k=1
3.计算定积分Q(k)
4.然后朝着inf/-inf或者歧义点移动一个步长,然后计算定积分
Q(k+1)
5.判断(abs(Q(k+1)-Q(k))
Mathematica中可以使用NIntegrate[],对于无穷振荡的函数,可以使用Method->
QuasiMonteCarlo或者Oscillatory]
=================================== - [返回]
39).为什么我的Matlab程序这么慢?
:#FangQ(Qianqian.Fang@dartmouth.edu), 2002/6/22. BigGreen/en_Matlab#
我们工学院的收发室的门上贴着一张小纸条,写的是
"Our policy is always blaming the computer"
大多数的人在遇到问题的时候,总是责备计算机如何如何,别人如何
如何,其实,最最主要的因素是在于自己。
一个程序运行快慢,有很多因素决定,最主要的是算法,简炼而优美
的的数学公式胜过100遍的优化。能从算法上改进,才能比别人有根
本的优势。计算机也很重要,以前我总把自己用的PC看成万能的加
以崇拜,对UNIX嗤之以鼻,结果当自己真正开始算起来,才知道差
别有多大。搞大型数值计算的,没有好的工作站或者并行系统,就输
在了起跑线上了。然后是程序的优化,看看变量是否占用太多内存,
看看是否有功能重复的模块或者计算,经常的是用牺牲内存来换取速
度,具体取舍,具体需要来决定。用profile看看哪些语句占用时间最
多,然后把核心部分进行优化。
如果是使用Matlab,使用vectorization和矩阵整体操作的代码要比大量
的for循环快很多,eval/inline函数如果出现在核心循环,也会让速度下
降几时倍的。