Matlab useful tips

来源:互联网 发布:立体设计软件新手 编辑:程序博客网 时间:2024/05/18 09:47

A([1 2 3]) % [] as a set of subscripts
x=[1 2 3] % [] as vertcat
x=[1;2;3] % [] as horzcat


1) define variable
x=5

2) list all variables
who/whos

3) clear all variables
clear

4) size() function


5)What does y(:,1) mean? (Matlab)

If you define a bidimensional array y, and you want to access all its elements on the first column:

y(:,1)

will do it.

If you want to access to all the elements of the fift row:

y(5,:)

is the syntax you have to use. The colon means: take all the elements along the specified dimension.
--Franco 18:36, 29 September 2006 (PDT)

6)intersect

7)sub-array:
array(index1:index2)

8)vertcat

9)
 disp(sprintf('pairlist count = %d', N));
 
 
10) use 3D matrix

    RX=zeros(4000,2, paramcount);


    % need to be extact size of 3D matrix element
    if(size(RR,1)<=4000);
      RR=vertcat(RR,zeros(4000-size(RR,1),size(RR,2)));
    end;
    RX(:,:,row_idx) = RR;
   
11) plot date and price

x=datenum(...);
y=price list;
plot(x,y);
datetick('x','yyyy-mm-dd')

12) merge 2 return series with date matching (merge R2 into RR)

-------------------------------------------------------
-- method 1: hard loop
-------------------------------------------------------
if(isempty(RR))
    RR=R2;
else
    [m,n]=size(RR); %get dimensions of new matrix
    for v=1:length(R2(:,1)) %loop through data to merge
    indx=find(RR(:,1)==R2(v,1)); %find existing dates/labels
    if indx
        RR(indx, 2) = RR(indx, 2) + R2(v,2);
        matchcnt=matchcnt+1;
    else
        RR(m+1, :) = R2(v, :);
        m = m+1;
    end
    end
    RR=sortrows(RR); %sort the new matrix
end

       
-------------------------------------------------------
-- method 2: use intersect and setdiff
-------------------------------------------------------
x=[
1 10
2 20
3 30
4 40];

y=[
1 7
3 9
6 60
8 88];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% merge two matrix: sum up column 2 values base on column1
[~,xc,yc]=intersect(x(:,1), y(:,1));
[~,app]=setdiff(y(:,1), x(:,1));
result=[x; y(app,:)];
result(xc,2) = result(xc,2) + y(yc,2);

result    
       
13) hash map

%% create map
k={'one','two'}
v={1,2}
m1=containers.Map(k,v)


%% get value
m1('one')

%% get invalid key raise error
m1('on')

%% show all keys
keys(m1)

%% show all values
values(m1)

%% add new item
m1('three')=3

%% get item count
m1.Count

%% validate key
isKey(m1, 'abc')

%% remove item
remove(m1, 'one')


14) remove zero value rows

 m=[1 0 0; 0 0 0; 2 3 4];
 m(all(m==0,2),:)=[];
 
 
15) find last non-zero index
x=[0 0 1 2 3 0 6 8 0 0];
fidx=find(x, 1, 'last');


16) example: if results.adf < results.crit(2) means that there is
a better than 95% probability that the series is stationary
(if x is y/t, then y and t are co-integrated)

function results =  adfPS(x,p,l)
% PURPOSE: carry out DF tests on a time-series vector
%---------------------------------------------------
% USAGE: results = adf(x,p,nlag)
% where:      x = a time-series vector
%             p = order of time polynomial in the null-hypothesis
%                 p = -1, no deterministic part
%                 p =  0, for constant term
%                 p =  1, for constant plus time-trend
%                 p >  1, for higher order polynomial
%         nlags = # of lagged changes of x included          
%---------------------------------------------------
% RETURNS: a results structure
%         results.meth  = 'adf'
%         results.alpha = estimate of the autoregressive parameter
%         results.adf   = ADF t-statistic
%         results.crit = (6 x 1) vector of critical values
%                        [1% 5% 10% 90% 95% 99%] quintiles   
%         results.nlag = nlag  
%---------------------------------------------------
% SEE ALSO: prt_coint()
%---------------------------------------------------
% References: Said and Dickey (1984) 'Testing for Unit Roots in
% Autoregressive Moving Average Models of Unknown Order',
% Biometrika, Volume 71, pp. 599-607.


17) left shift array to calculate close/close return
method 1:
x=[20 30 40 50];
y=circshift(x',3)' % y:30    40    50    20

method 2:
y=[x(2:end) x(1)];



18) split delimited string

% sort the system path
xx=regexp(path,';','split')'
sort(xx)


19) join two matrix base on column1
x=[
1 10
2 20
3 30
4 40];

y=[
1 7
3 9
6 60
8 88];

[~,xc,yc]=intersect(x(:,1), y(:,1));
[~,app]=setdiff(y(:,1), x(:,1));
joinres=[x [y(yc,2);zeros(size(x,1)-length(yc),1)] ]; %add intersect y columns on x (right side)
joinres=[joinres; [y(app,1) zeros(length(app),1) y(app,2)]]; %append diff y columns at bottom side
joinres

20) add row index
x=magic(6)
y=[(1:size(x,1)).' x]


21)generate repeated number

%-- method 1
x=zeros(1,10);
x=x+5;

%-- method 2 (best)
x=rempat(5, 1, 10);

%-- method 3 (use more memory)
val=5;
x=val(ones(1,10)); % is equal to x=val * (ones(1,10))


22)read date string from file
% Date,OPEN,HIGH,LOW,LAST_PRICE,VOLUME,PX_OFFICIAL_AUCTION,OFFICIAL_AUCTION_VOLUME,EQY_WEIGHTED_AVG_PX
fid = fopen('data1.csv', 'rt');
a = textscan(fid, '%s %f %f %f %f %f %f %f %f', ...
      'Delimiter',',', 'CollectOutput',1, 'HeaderLines',1);
fclose(fid);

data = [datenum(a{1}) a{2}];


23) return max value along with index
[Y,I] = MAX(X) returns the indices of the maximum values in vector I.
If the values along the first non-singleton dimension contain more
than one maximal element, the index of the first one is returned.

24) locate vector by date
% first column is datenum
data(find(data(:,1)==datenum('2008-03-18')),:)


25) (from array tricks)
impl of repmat:

sz=[m n];

%-- original method
x=repmat(val, sz);

%-- manually
x(prod(sz))=val;
x=reshape(x,sz);
x(:)=val;


26) shift rows
1 2
3 4
5 6

==>

3 4
5 6
1 2

yy=[x(2:end,:); x(1,:)]


27) help paen ()
If X has N components, X(N:-1:1) reverses them.
X(X>0.5) returns those elements of X that are > 0.5. (X>0.5) return logical vector


28)manual convert matlab datenum and excel datenum
matlabDateNum - excelDateNum = 693960

29)replicate: [4 5] = [4 4 4 5 5 5];
N=3;
x=[4 5];
y=x([1 1 1], :); % y=x(ones(1,N), :);
y=y(:).';

%-- method 2
y=a(ceil((1:N*length(x))/N))



30) use cell to get sub-matrix
>> data=magic(6)
    35     1     6    26    19    24
     3    32     7    21    23    25
    31     9     2    22    27    20
     8    28    33    17    10    15
    30     5    34    12    14    16
     4    36    29    13    18    11

>> xdim={[2 3], ':'}
>> data(xdim{:}) % equal to [data(2,:), data(3,:)]
ans =
     3    32     7    21    23    25
    31     9     2    22    27    20
   
31) rotate 90 degree
y=x(:,end:-1:1).';


32) linear indexing
access matrix elements as a column vector

33) display large integer
fprintf('%.0f/n', val);


34) function variables
function variables are not shown in matlab workspace, only limit to function body


35) find first non-zero element in each column
% x is the matrix, have at least one non-zero value at each column
[i,j,v]=find(x);
k=diff([0;j]);
i(logical(k));

% might contain all zero in
[i,j,v]=find(x);
k=logical(diff([0;j]));
p=repmat(nan,size(x,2),1);
p(j(k))=i((k));



36) calculate moving average
% -- method 1
ma1=tsmovavg(data,'s',ma_size,dim); % for column vector, dim=1

% -- method 2;
ma1=filter(ones(1,ma_size)/ma_size, 1, data);

37) calculate moving standard deviation

x=rand(10,1);
rollma=filter(ones(1,N)/N, 1, x);
rollvar=(filter(ones(1,N),1,x.^2)-N*rollma.^2)/(N-1);
rollstd=sqrt(rollvar);

38) specific X-axis datenum element count
set(gca,'XTick', dlist(1):(length(dlist)-1)/100:dlist(end));





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% append 2011 July 17
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

41) remove rows if contains not-finite
data = [1 2;3 4; nan 5; 6 nan; nan nan];
data(any(~isfinite(data),2),:)=[]

42) to read Excel date effectively, change the date in numeric format and use xlsread() in matlab,
after that, add 693960 to matlab datenum.


43) find first number > 60
x=sort(randi(100,[10,1]));
x(find(x>60,1,'first'));

44) remove bloomberg fetch data warning
warning off datafeed:bloomberg:Deprecation;

45) calculate winning count(count value > 0)
%val=(date,pnl);
wincnt=sum(val(:,2)>0);

46) calculate total win profit (sum value > 0)
sum(val(val(:,2)>0, 2))

47) display datenum(big integer) and return value(small float)
format longG

48) plot two curve at the same time
plot([x,y]);
plot(x,y); % Error!

49) linkaxes
ax(1) = subplot(2,1,1);
plot([BundClose,lead,lag]); grid on
ax(2) = subplot(2,1,2);
plot([s,cumsum(r)]); grid on
linkaxes(ax,'x')