Labels

_fuxi (75) _IV (146) _misc (5) {610610 (30) algo (1) automatedTrading (8) banking/economy (3) book (14) c++misc (125) c++real (15) c++STL/java_container (7) cppTemplate (1) db (13) DB_tuning (4) deepUnder (1) dotnet (69) eTip (17) excelVBA (12) finance+sys (34) financeMisc (24) financeRisk (2) financeTechMisc (4) financeVol (21) finmath (17) fixedIncome (25) forex (16) IDE (24) invest (1) java (43) latency (4) LinearAlgebra (3) math (30) matlab (24) memoryMgmt (11) metaPrograming (2) MOM (15) msfm (1) murex (4) nofx (11) nosql (3) OO_Design (1) original_content (4) scriptUnixAutosys (19) SOA (7) socket/stream (15) sticky (1) subquery+join (2) swing (32) sybase (6) tech_orphan (12) tech+fin_career (30) telco (11) thread (21) timeSaver (13) tune (10) US_imm (2) US_misc (2) windoz (20) z_algo+dataStructure (4) z_arch (2) z_c#GUI (30) z_career (10) z_career]US^Asia (2) z_careerBig20 (1) z_careerFinanceTech (11) z_FIX (6) z_forex (31) z_hib (2) z_ikm (7) z_inMemDB (3) z_j2ee (10) z_oq (14) z_php (1) z_py (26) z_quant (4) z_skillist (3) z_spr (5)
Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Friday, March 13, 2015

matlab | index of first positive value

See the stoch HW8 on Poisson process, where our array has timestamps of each consecutive jump, and we need to find how many jumps by time=2 minutes.

Thursday, June 12, 2014

matlab | string vs cell of 1 string

Many matlab functions work with a string OR a cell of 1 string. So sometimes we don't realize their difference.

You can check the exact type of given stringy thingy with the function class().

To convert a cell into a string, use theVariable{:}

eg: regexp() --> cell

Saturday, June 7, 2014

matlab | find()

I feel a lot of textbooks skip this instrumental function, and other tutorials on this function are not focused. Let's keep things very simple and focus on the bare essentials.

Focus on a vector, not a matrix.

Focus on find(some logical expression) rather than find(someVector)

http://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html says
Logical indexing is closely related to the find function. The expression A(A > 5) is equivalent to A(find(A > 5)). Therefore, better learn logical indexing first.

matlab | logical subscripting - learning notes

http://www.mathworks.com/help/matlab/learn_matlab/indexing.html#f2-15124 clearly defines it -- "Suppose X is an ordinary matrix and L is a matrix of the same size that is the result of some logical operation. Then X(L)specifies the elements of X where the elements of L are nonzero."

Note if L has 5 non-zero elements, then length(X(L)) == 5.

I think L must be an array of booleans, not doubles.

For a matrix, see http://www.mathworks.com/help/matlab/math/matrix-indexing.html#bq7egb6-1

But here's a real illustration in my code:

  step = 1/200;
  steps = 2/step;
  reruns=500;

  % generate increments
  %rng(0,'twister'); % if we want repeatable
  incr = randn(steps,reruns)*sqrt(step);

  std(incr) % should  all be around 0.07
  hist(incr(:,1))

  % random walker positions
  p = cumsum(incr);

  % select a subset of Columns, using filter on
  % "200th ROW and 400th ROW" so
  % row expression = wildcard; column expression = filter on Row.
  % If we carelessly swap the expressions, matlab won't warn us!
  qualified = p(:, (p(200,:)>0 & p(400,:)>0));

Tuesday, June 3, 2014

matlab | foreach loop on matrix

If your original matrix is a column vector, then you better transpose it before using foreach. For a given matrix, foreach takes one column at a time.

Sunday, June 1, 2014

matlab | assign to cell array

% assigning into 2 consecutive cells, using parentheses not braces
outputCell(tmp_newRow, 3:4) = num2cell(betaTukeyN)

% assign to individual cell, using braces, not parentheses
outputCell{end+1, 3} = betaTukeyN(1)

matlab | delete matrix rows based on specified fields

matlab | a few useful indexing techniques

http://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html shows --

extract all the odd elements
extract every 3rd element
Reverse the order of elements

--logical subscript
To replace all NaN elements with zero

Saturday, May 31, 2014

matlab | sscanf faster than str2double

trFolder = 'data\mmm\SH600519T';
trFiles = dir(fullfile(trFolder, 'trade*2013013*.csv'));
tr1D =read1csv(fullfile(trFolder, trFiles(1).name));
tic
for i=1:length(tr1D.textdata(:,4))
tt=tr1D.textdata(i,4);
dummy = sscanf(tt{:}, '%f');
end
toc
%%%%%%%%%%%
tic
str2double(tr1D.textdata(:,4));
toc

Tuesday, May 20, 2014

matlab | append to an array

b_mkt(end+1)=1

matlab | index of max item in an array

[max_value, index] = max(r2);

matlab | iterate over an array

for elm = list
%# do something with the element
end

matlab | str2double converts array of strings

cell2mat seems to be for another purpose

Saturday, May 3, 2014

matlab | swap x and y axist

x = 0:.01:pi ; plot(x,sin(x),'b-') ; % example plot
view(-90,90)
set(gca,'ydir','reverse')

Saturday, April 26, 2014

matlab | join 2 matrices horizontally

regressor2 = horzcat(factor704, bondRa(:, 1));

Saturday, March 22, 2014

Matlab | clear all except breakpoint

tmp = dbstatus;
save('tmp.mat','tmp')
clear classes % clears even more than clear all
load('tmp.mat')
dbstop(tmp)
% clean up
clear tmp
delete('tmp.mat')

Wednesday, February 26, 2014

Matlab | extracting string from in a cell array

A(1,1) is still a cell array -- 1x1. It's not a string and many functions won't accept it.

myStr = sprintf('%s',A(1,1));

Saturday, February 22, 2014

matlab [] vs ()

paren and brackets are by far the most versatile constructs in matlab. Each has rich contextual meanings. Here is an incomplete sketch.


--Matlab doc on "special characters" --

Brackets are used to form vectors and matrices.

Parentheses are used to enclose subscripts of vectors

--http://stackoverflow.com/questions/5966817/difference-between-square-brackets-and-curly-brackets-in-matlab

A right angle (square) bracket creates a vector or matrix, whereas curly brackets creates a cell array.

When working with numbers, I'd say that 99% of the time, you will use square brackets. Cell arrays allow you to store different types of data at each location, e.g. a 10x5 matrix at (1,1), a string array at (1,2).

matlab | assign to multiple variables at once

  tmp = num2cell(array_of_values);

  [j, g, tmpM, tmpL, s] = tmp{:};

matlab | clear all variables like tmp_*

clear tmp_* % will not clear a_tmp or tmp123