Using a Function handle in Matlab - matlab

I would like to use knnimpute to fill some missing values in my dataset. Thing is, I would like to use my own distance function, instead of the typical ones (Euclidean, Manhattan...).
For what I've read, knnimpute allows me to use a function handle, that calculates the distance according to Heterogeneous Euclidean-Overlap Metric (HEOM)
I've implemented this function as a regular function, but not as a handle function. So, I cannot use the distance matrix from my "normal" function, because this has to be done inside knnimpute, somehow, as a handler...
I'm confuse, can someone help me understand what I need to do?

As long as your implementation of a distance function has the same signature as the standard distance functions, then you should be able to easily pass your function in.
From the knnimpute documentation (matlab knnimpute) it states that you can pass "A handle to a distance function, specified using #, for example, #distfun." It then refers the reader to the pdist function which provides more details (matlab pdist) about the custom distance function:
A distance function specified using #:
D = pdist(X,#distfun)
A distance function must be of form
d2 = distfun(XI,XJ)
taking as arguments a 1-by-n vector XI, corresponding to a single row of X, and an m2-by-n matrix XJ, corresponding to multiple rows of X. distfun must accept a matrix XJ with an arbitrary number of rows. distfun must return an m2-by-1 vector of distances d2, whose kth element is the distance between XI and XJ(k,:).
So as long as your distance function, as defined in your *.m file matches this signature and so can support these inputs, then there shouldn't be any problems.
Suppose that your distance function is in the mydistFunc.m file, and it's signature matches the above requirements, then all you should need to do is:
% call knnimpute with the data and your function
knnimpute(inputData,'Distance',#mydistFunc);

Related

How to compute Hessian of a function defined by a multidimensional array

I have an array defined in MATLAB with dimensions 5×5×5×5, i.e., it is a 4-dimensional array. The numbers of this array define the values a function of 4 variables takes over some domain.
I need to compute the Hessian of this function. I need to compute it at every point in the array (ignoring corner cases, say). To do this, I take the function value at a point, compute the second differences and cross-partials across different dimensions. Then I create a determinant and examine its sign.
How to automate this process? That is, visit each point, and compute the determinant, using diff function in MATLAB? Is there a defined procedure?

Matlab fit with function returning vector with all the y(x_i) values

I am working on something and I haven't found any solution, maybe I didn't know how to correctly search for it...
I have two arrays of experimental data (x and y). x is a list of certain energies (512 values from 0 to 100 kev) and I want to fit them to a function which returns a vector of values of y for every x in the list (the energies are always the same, 512 certain values). This is because my function model contains several matrix and other functions.
So, I can't evaluate my function as f(x,a,b,c...) (with a,b,c the parameters to fit) and expect a single scalar, but I have to evaluate f(a,b,c...), and it returns a vector of y(x1),y(x2)...
Now, I want to fit my data to my model. But lsqcurvefit needs a function of the form f(x), I suppose that it evaluates every f(x). I could write my function so that every time it is called it evaluates the vector result, and then returns y for the given x, but it would be quite inefficient... And I'm sure there must be another way.
Any idea?
Maybe you can do an fminsearch on the sum of square errors? It is best to put all fitting parameters into one vector. Here I call it p.
f = #(x,p) (p(3)+p(1)*x.^p(2)).^(1/p(4); %example function with four free parameters
sqerr = #(x,y,p) sum((y-f(x,p)).^2); %sum of squared errors
p = [1,1,1,1]; %four starting conditions
p = fminsearch(#(p) sqerr(x,y,p),p); %fit
Then you can find your y(x_i) values by calling the function with the fitted paramters
f(x,p)

Create a function handle within a structure, given some coefficients

I have a vector of coefficients available, which are the coefficients of an interpolating polynomial. I want to create a field in a structure as follows:
p.val =#(y) polynomial
Here polynomial is the polynomial with my coefficients in the indeterminate y. I'm not sure how to do this. The field has to contain a function handle like this.
The user has to be able to call this as follows:
pval = p.val(y)
where, y can be a vector of any length, supplied by the user (which contains values where the polynomial needs to be evaluated). So, this output has to be a column vector, such that every cell contains the specific function value.
Any help on how this can be done is appreciated!
I am not sure what function you want to implement, but the length of the vector you are including does not matter.
example.a = 3; % Any variable
example.b = #(s) mean(s);
The function mean here can be anything from a built-in function to a custom function you just made.
You can call it by:
result = example.b(rand(1000,1);

apply function to each column of a matrix (Vectorizing)

What is fastest way of applying function on each column of a matrix without looping through it?
The function I am using is pwelch but the concept should be the same for any function.
Currently I am looping though my matrix as such.
X = ones(5);
for i = 1:5 % length of the number of columns
result = somefunction(X(:,i))
end
Is there a way to vectorize this code?
You say
the concept should be the same for any function
Actually that's not the case. Depending on the function, the code that calls it can be made vectorized or not. It depends on how the function is written internally. From outside the function there's nothing you can do to make it vectorized. Vectorization is done within the function, not from the outside.
If the function is vectorized, you simply call it with a matrix, and the function works on each column. For example, that's what sum does.
In the case of pwelch, you are lucky: according to the documentation (emphasis added),
Pxx = pwelch(X) returns the Power Spectral Density (PSD) estimate, Pxx, ...
When X is a matrix, the PSD is
computed independently for each column and stored in the corresponding
column of Pxx.
So pwelch is a vectorized function.

Custom distance (histogram intersection) function in Matlab knnclassify

Is it possible to implement a custom distance measure in Matlab "knnclassify" function?
In particular, I am interested in classifying an example according to the distance between two vectors to be equal to histogram intersection (vectors are considered to be histograms). For two N-dimensional vectors, w1 and w2, the distance is:
dist(w1, w2)=sum_i_to_N min(w1(i), w2(i))
By examining the source of knnclassify, this relies on using knnsearch. The parameter of the distance to use is supplied to this function when you look at the knnclassify source. By looking at knnsearch, you certainly can implement this function yourself. knnsearch allows you to specify a custom function as long as it can take in only two vectors of the same size. These vectors are from the data sets that you are applying knnclassify to. As such, create a new function or you can do it anonymously using either:
function [d] = histogramIntersection(w1, w2)
d = sum(min([w1,w2],[],2));
... or you can do this anonymously:
f = #(w1,w2) sum(min([w1,w2],[],2));
However, what you're going to have to do to incorporate this into knnclassify is that you will have to modify the source and include an additional condition in the switch statement so that you can include histogram intersection as a choice. Once you do that, you can either provide #f as input into the knnsearch call, or make some room in the code and define a histogramIntersection method like above, then use #histogramIntersection as input into knnclassify. This input should replace the string that is input into knnclassify that specifies the kind of distance measure you want.
tl;dr: You can do it but you'll have to modify the knnclassify source if you want to do this. Alternatively, you can see what knnclassify is doing, then just pull out the relevant calls that pertain to just your case and place your custom histogram intersection method accordingly, create a new file and just run this file. That way you don't need to mess with MATLAB's original source.