# Parzen window density estimation in MATLAB

This was my C# implementation of Parzen Window Density Estimation.

The following is my implementation of the same in Matlab:

parzen.m

function [retval] = parzen (matrix, dataPoint, variance)
[r c] = size(matrix);
A = ones(r, c).*dataPoint;
sub = matrix - A;
up = sub.^2;
dw = 2 * variance;
firstPart = 1/(sqrt(2*pi*variance));
retval = firstPart * exp((-1)*(up/dw));


Indentation is automatically fixed in MATLAB by marking everything, and do Ctrl+I.

The common practice is to have the first level alligned all the way to the left. Click here to see a screenshot from the builtin MATLAB function ind2sub. I have made no edits to it.

matrix is not a very good variable name. I suggest you choose something else. I don't have a suggestion off the top of my head, but there are probably more descriptive names.

[r c] = size(matrix);


There's not much more to say, best practice is to separate output values with commas.

The following subtracts the value of datapoint from A. I'm assuming datapoint is a scalar, not a matrix.

[r c] = size(matrix);
A = ones(r, c).*dataPoint;
sub = matrix - A;


This can be simplified a lot. If you want to subtract one value from all elements of a matrix in MATLAB, you can just do:

sub = matrix - dataPoint;


If dataPoint is a matrix, of the same dimensions as matrix, then you can do:

sub = matrix - dataPoint


Yes, that was exactly the same as above. As long as the dimensions match, or one of the variables is a scalar, you can simply subtract one from the other.

I guess dw means down? If so, call it down, it's easier to understand, and is not very hard to write.

You mix camelCase and lowercase. I suggest you stick to one, and do dataPoint and returnValue.

You forgot end in the end. It works without it, but it is supposed to be there.

Is it correct?

I don't know. I don't know what it's supposed to do. I guess the simplest way to check it is to compare it to your C# implementation.

• "The common practice is to have the first level alligned all the way to the left (as I've done in the screen shot below)." Even the part inside the function declaration? Either I'm reading this wrong or you're proposing a code smell.
– Mast
Nov 17 '16 at 13:09
• @Mast, yes, even inside function declarations. Have a look at this builtin MATLAB function. I guess it's a code smell in other languages, but it's default in MATLAB, and the editor automatically "fixes" it for you if you want. Nov 17 '16 at 13:18
• I might add that there's (almost) never more than one function inside each file. So there won't be any confusion... :) Nov 17 '16 at 13:23