Table of Contents

- Assign the same matrix to all elements of a struct array
- Multiply a 3d matrix with a vector to get a 2d matrix
- Concatenate a cell array of matrices to one 3d matrix
- Find a Row in Another Matrix
- Preallocate a Cell Array of Full Matrices and Sparse Matrices
- Set the value at many (row,column) index pairs in a matrix
- Create new matrix B where each column in A is repeated k times:
- Delete All-Zero Columns or Rows
- Find n smallest values in a matrix and return their indices
- Normalize rows of matrix to sum to 1
- Normalize each row or column to length one
- To free forgotten file handle locks
- Read in text file
- Plot figure to a printable pdf of correct size
- cell2str
- Speed
- Frequently compare times of different functions
- Simple Parallelization
- Good Optimization Package
- General Matlab Speed-up Packages
- Plot a tree with labels on each node
- Convenient File Reading
- Read in Sparse Index:Value matrix
- Convenient Text File Writing of Matrices and String Cells
- Extract Sentences from a Wall Street Journal tree file
- Find out how many matlab licenses are being used.
- Saving Only a Matrix to PNG File
- Saving an image from imshow without a white/grey background
- Fast Word to Number Mapping with Java Map Containers
- Find all dependencies of a function
- Visualizing a Bivariate Gaussian Distribution
- Sort a cell array based on the length of each cell
- Element-wise max of all elements in a matrix
- Subassign a smaller matrix into a larger matrix at a range of indices
- Compute the Probability of a Point being an outlier
- Comments, Suggestions

Assign the same matrix to all elements of a struct array

Multiply a 3d matrix with a vector to get a 2d matrix

Concatenate a cell array of matrices to one 3d matrix

Find a Row in Another Matrix

Preallocate a Cell Array of Full Matrices and Sparse Matrices

Set the value at many (row,column) index pairs in a matrix

Create new matrix B where each column in A is repeated k times:

Delete All-Zero Columns or Rows

Find n smallest values in a matrix and return their indices

Normalize rows of matrix to sum to 1

Normalize each row or column to length one

To free forgotten file handle locks

Read in text file

Plot figure to a printable pdf of correct size

cell2str

Speed

Frequently compare times of different functions

Simple Parallelization

Good Optimization Package

General Matlab Speed-up Packages

Plot a tree with labels on each node

Convenient File Reading

Read in Sparse Index:Value matrix

Convenient Text File Writing of Matrices and String Cells

Extract Sentences from a Wall Street Journal tree file

Find out how many matlab licenses are being used.

Saving Only a Matrix to PNG File

Saving an image from imshow without a white/grey background

Fast Word to Number Mapping with Java Map Containers

Find all dependencies of a function

Visualizing a Bivariate Gaussian Distribution

Sort a cell array based on the length of each cell

Element-wise max of all elements in a matrix

Subassign a smaller matrix into a larger matrix at a range of indices

% example subMatrix
smiley = zeros(5,5);
smileyPos = [4,1;5 2; 5 3;5 4;4 5;2 2; 2 4]
for p = 1:size(smileyPos,1)
    smiley(smileyPos(p,1),smileyPos(p,2)) = 1;
end

% full image
fullImg = zeros(15,15);

% range of positions where subMatrix should be:
r1 = [1:5];
r2 = [11:15];

% example call of function
fullImg = assignSubMatrix(smiley,r1,r2,fullImg);


function fullMatrix = assignSubMatrix(subMatrix,indexBlock1,indexBlock2,fullMatrix)
% Richard at Socher.org

subMatrix=subMatrix' ;

X = indexBlock1(ceil([1:length(indexBlock1)*length(indexBlock2)]/length(indexBlock2)));
Y = repmat(indexBlock2(:), [1 length(indexBlock1)]);
setProd = [X(:) Y(:)];

fullMatrix(sub2ind(size(fullMatrix),setProd(:,1),setProd(:,2))) = subMatrix(:);
 

Compute the Probability of a Point being an outlier

%loOP_script

% use this many points to define the neighborhood
kNN = 50;

lambda = 3;

% needs slmetric_pw
addpath(genpath('slmetric_pw'))

load('ex8data1.mat');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Compute Local Outlier Probabilities  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for data points X (trained mapped images later)
% each point is one row of this matrix

%compute all nearest neighbors
allDist = slmetric_pw(X',X','eucdist');

%first row are just the points, then follow the nearest neighbors
[allPointsNNDistances allPointsNN] = sort(allDist);

S = allPointsNN(2:kNN+1,:);
Sdist = allPointsNNDistances(2:kNN+1,:);

sigma = sqrt(sum(allPointsNNDistances.^2)./kNN);

pdist = lambda * sigma;

% expected value: E_{s\in S(o)}[pdist(s,S(s))]
Epdist = mean(pdist(S));

plof = pdist./Epdist -1;

nplof = lambda * sqrt(mean(plof.^2));

loop = erf(plof./(nplof * sqrt(2)));
loop(loop<0) = 0;


% outlier threshold:
outliers = loop>0.7;

%  Visualize the example dataset
fprintf('Visualizing example dataset for outlier detection.\n\n');
hold on
plot(X(:, 1), X(:, 2), 'bx');
axis([0 30 0 30]);

plot(X(outliers, 1), X(outliers, 2), 'ro', 'LineWidth', 2, 'MarkerSize', 10);
hold off
 

Comments, Suggestions

Add Comment 
Sign as Author 
Enter code: