matlab cheat sheet

–to insert a column into a matrix…. create a new matrix with the new column and the existing matrix.
–format long g % to display more digits without “e”

%{
multi-line comment
%}

nan(N) % better error detection than
ones(N)
zeros(N)
–Calling another .m script (not a function) — http://stackoverflow.com/questions/5226840/call-a-matlab-script-in-a-script

— print variable with tag
disp([‘x is equal to ‘,num2str(x),’.’])
fprintf(‘TS: row # %in’, foundInHeet);

–“die”

    error(‘Every time stamp must match between EWJ/JPP time series’)

— execute a multi-line selection of code
select -> right-click -> evaluate selection
–locate nan’s in a large vector
find(isnan(yourarray))
–code folding
preferences -> editor/debugger

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));