搜索此博客

2010年8月26日星期四

循环 矩阵


for m=1:5
mzero{m}=zeros(2,3);
end 

2010年8月23日星期一

[原创]matlab 数据处理 新得体会

本来ns2是主要奋斗目标,结果最近总是用matlab来处理数据。

线说一下awk,不知道是不是不会用,matlab跑20s的东西,那里要处理3个小时...

Oh My God...转会matlab 其实matlab还是挺强大的!

首先,matlab只要循环就很缓慢,所以一定要熟悉向量操作,.*啊 ./啊 之类的 包括比如给一个index向量,本来是[1 2 3 4... N]以前第一想法就是循环! 千万别

有一个a=1:1:10000; 非常迅速

然后如果实在需要循环,也就是我最近苦恼的问题,实在必须循环的话,就要多用tic toc,看看数值上到底都用了多少秒解决的,然后就着一个比较reasonable的来弄。


比如:
我是time series画图,所以每个时间点的数值都要计算,必须只能是循环...那怎么办呢?而且循环到i很大一次很费时间。所以我们把后面的分段,每次都还是循环那个一点点 就好了。

2010年8月22日星期日

matlab文件读写 读写数据类型:矩阵 ascii

俗话说的好使用要配套!

ascii的好处就是直接打开就能看 不象binary二进制数字乱码



首先读数据: load('filename.ext');

写数据用save,我们喜欢用可以filename是个变量名儿的那种,help save里面有一条这样写着

save(savefilename,'data');

经过尝试,如下可以写入ascii的格式,例

A=[1 2 3 ; 4 5 6; 7 8 9];
filename=['hello1-' num2str(123) '.txt'];

save(filename, 'A', '-ascii');

这样呢就是把矩阵A写入到文件名为 hello1-123.txt里面

matlab 读写文件 文件名里面有读写的办法

因为文件名里面有变量,所以我们利用matlab里面最为经典的中括号原理

比如
a=1;
b=2;
c=[1 2];
如果1 和2 是str,那么c就是两个str的合并了!
数字与字母的转换为 num2str这个函数。

目前为止,还是尽量使用数字,因为一个是字母没法做循环,还有一个是字母非常的造成大型程序很麻烦。

下面是简单的实现,ref某学长,感谢DP学长的耐心指导!


%首先,使用fopen打开一个文件
%文件名是你给该文件的命名,a是mode,详细查matlab help

fid=fopen('filename.txt','a');

% 把需要输入的数据inputData打印在fid实例,也就是filename.txt里面
fprint(fid,'%f\n',inputData);

fclose(fid); % close filename.txt

好,如何实现文件名称变量化呢?我们需要把'filename.txt'使用一个变量来代替,比如filename='filename.txt'

然后filename这个字符串使用[]中括号方式,把字符串在一起,这个串联的过程,就可以出现数字,记得使用数字时候,不要忘记num2str.

好了,现在可以对文件名进行变量化了。这种方法还可以实现任何想要变量化的字符呢!原理你当然是知道的了!

超级啰嗦的温馨提示:一定要注意哦 []的用法是前后要空格,所以变量一样要空格

如:如果想要命名文件名为05-1.txt

那么例如i是循环系数,可是取值1,2,3, 某string变量ss=['05-' num2str(i) '.tr']

千万不要忘了num2tr(i)前后是有空格的呦 要不然汇报错!

OK 完毕!




ref:某学长 嘻嘻 人很好的!嘻嘻^^

2010年8月18日星期三

图书馆里面的书 - maybe useful

Introduction to design and analysis of experiments (QA279 COB (ORDINARY)

Basics of structural equation modeling (QA280 MAR (ORDINARY)



Variance components 
QA279 SEA (ORDINARY)


Confidence intervals on variance components  QA276.74 BUR (ORDINARY)


Estimation of variance components and applications  QA279 RAO (ORDINARY)


The analysis of variance  G70.3 CAT/30 (ORDINARY)     H61 QUA/12 (ORDINARY)    H61 QUA/1 (ORDINARY)


Handbook of statistics / Vol. 1, Analysis of variance   QA276 HAN/1 (ORDINARY)


Applied statistics, analysis of variance and regression  QA279 DUN (ORDINARY)


Experimental design and analysis  QA279 LEE (ORDINARY)




The analysis of variance : a basic course   QA279 HUI (ORDINARY)











matlab load&save


% Load the file to the 
% matrix, M :
 M = load('sample_file.txt') 
 

% Add 5 to M :
M = M +5 


% Save M to a .mat 
% file called 
%'sample_file_plus5.mat':
save sample_file_plus5 M


% Save M to 
% an ASCII .txt file 
% called 'sample_file_plus5.txt' :
save sample_file_plus5.txt M -ascii 

2010年8月16日星期一

对数函数公式表

logaMN=logaM+logaN
logaM/logaN=logaM-logaN
logaM^n=nlogaM
logbN=logaNb/logab
logaB乘logbA=1
logaB*logbC*logcD=logaD
loga(m)b(n)=n/mlogaB

2010年8月11日星期三

MatLab * / .* ./

Need to notice that

.*  : Array multiply
*   : Matrix multiply

./  : Right array divide  
/   : Slash or right matrix divide

Example:

coef = zeros(1,1001);
 coef(1) = y(1);
 y = (y(2:1001)-y(1:1000))./(x(2:1001)-x(1:1000));



ref:
http://www.cyclismo.org/tutorial/matlab/operations.html

matlab 加速

The ability to work with these vector functions is one of the advantages of Matlab. Now complex operations can be defined that can be done quickly and easily. In the following example a very large vector is defined and can be easily manipulated. (Notice that the second command has a ";" at the end of the line. This tells Matlab that it should not print out the result.)
>> x = [0:0.1:100]

x =

  Columns 1 through 7 

         0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000

    [stuff deleted]

  Columns 995 through 1001 

   99.4000   99.5000   99.6000   99.7000   99.8000   99.9000  100.0000


>> y = sin(x).*x./(1+cos(x));
ref:
http://www.cyclismo.org/tutorial/matlab/operations.html

vector operation in MatLab - how to read portions of a vector

v = [0:2:8]

v =

     0     2     4     6     8





>> v(1:3)

ans =

     0     2     4

>> v(1:2:4)

ans =

     0     4

ref: http://www.cyclismo.org/tutorial/matlab/vector.html

2010年8月6日星期五

mac os 显示隐藏 文件 文件夹

显示:defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏:defaults write com.apple.finder AppleShowAllFiles -bool false

2010年8月5日星期四

mac os 使finder在标题栏上显示完整目录

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
killall Finder















Reference:http://mac.linsheng.me/archives/515.html

Matlab常用画图语句

1. 画图
(1)普通画图:
plot(x-axis,y-axis,'color&style');
(2)横纵都为log图:
loglog(x-axis,y-axis,'color&style');
(3)横正常;纵log图:
semilogy(k09,pM09,'-r',k09,pT09,'^r',k07,pM07,'-b',k07,pT07,'^b',...
k05,pM05,'-k',k05,pT05,'^k',k03,pM03,'-m',k03,pT03,'^m');

2. 给图加注释
xlabel('what is on x-axis');
ylabel('what is on y-axis');
legend('1st line - name','2nd line - name','',...);

titlestr = 'title of this figure';
%图的标题有变量的时候
titlestr= ['M/M/1 steady state (\rho=',num2str(rho),')'];
title(titlestr);

hold on;
grid on;
set(gca,'YTick',[1e-12 1e-11 1e-10 1e-9 1e-8 1e-7 1e-6 1e-5 1e-4 1e-3 1e-2 1e-1 1e0]);