HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Parzen window density estimation in MATLAB

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
matlabdensitywindowparzenestimation

Problem

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


What do you think about this implementation? Is it correct?

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#146269, answer score: 4

Revisions (0)

No revisions yet.