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). Usesum(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 supportsforeach-like statement:
for element = list
% <statements>
endIf 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>))byisempty(find(<expr>, 1))to improve performance.
Libraries
VLFeat
- Use
vl_imreadjpeginstead ofimread. 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));