How to use anonymous function in linkage ?

I am trying to use my own function in linkage. https://www.mathworks.com/help/stats/linkage.html
I completely have no idea and this tutorial https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html does not help at all because the anonymous function in linkage has no inputs.
This is my code currently based on
test = linkage(array,'single','@custom');
This is how my custom distance is defined
function distance = custom(data_a, data_b)
distance = abs(data_a(1) - data_b(1)) + abs(data_a(2) - data_b(2))
end
can someone give me a good example of how I can call a @custom without any arguments ?

Réponses (1)

Walter Roberson
Walter Roberson le 17 Jan 2017
test = linkage(array,'single', @custom);

4 commentaires

I am getting an error
Error using linkage (line 128)
Third input must be a string or a cell array.
My array is 100 x 5 double. My custom distance function is in a custom.m file in the same directory.
Z = linkage(array, 'single', @custom);
function distance = custom(data_a, data_b)
w1 = abs(data_a(1) - data_b(1)) + abs(data_a(2) - data_b(2)) + abs(data_a(3) - data_b(3)) + abs(data_a(5) - data_b(5));
distance = w1 * w1 / (1/data_a(4) + 1/data_b(4));
end
I think
Z = linkage(array, 'single', '@custom');
with the apostrophe worked.
RuiQi
RuiQi le 18 Jan 2017
Modifié(e) : RuiQi le 18 Jan 2017
If i have the function in the same .m file then
Z = linkage(array, 'single', @custom);
gives me the error
Error using linkage (line 128)
Third input must be a string or a cell array.
and
Z = linkage(array, 'single', '@custom');
says
Error using pdist (line 349)
The distance function 'custom' was not found.
Error in linkage (line 247)
Z = linkagemex(Y,method,pdistArg, memEff);
Walter Roberson
Walter Roberson le 18 Jan 2017
Which MATLAB release are you using? The ability to use custom functions is not documented up to R2010b, but is documented starting with R2011a.

Connectez-vous pour commenter.

Tags

Question posée :

le 17 Jan 2017

Commenté :

le 18 Jan 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by