Matlab

General

  • Show the information of a variable. It is very useful when we the code takes so much memory: whos <variable name>.
  • Sum of squared of elements: sumsqr(X). Use sum(sum(A.^2, 1)) instead if you want to use less memory.
  • Memory optimizations tips and tricks: [Undocumented Matlab]
  • Use column-based representation instead of the row-based one. For example, to represent a list of items from 1 to 5, use [1 2 3 4 5] (not [1; 2; 3; 4; 5]). It becomes convient because MATLAB supports foreach-like statement:
for element = list
    % <statements>
end

If list is a matrix, the above statement will assign each column of list to element.

  • Construct one-shot vectors from label vector matlab one_shot = full(sparse(1:length(labels), double(labels), 1));

Code Optimization

  • Replace isempty(find(<expr>)) by isempty(find(<expr>, 1)) to improve performance.

Libraries

VLFeat

  • Use vl_imreadjpeg instead of imread. It could speed up reading image files.

Editor

Nvim

Plugins:

Data Structures

String

Find a string in a cell array

find(ismember(cell_array, str));
comments powered by Disqus