Question
I'm trying to do hierarchical clustering in MATLAB using 'linkage' and 'pdist' functions. I'm familiar with the functions, but I'm attempting to cluster by the absolute value of the correlation values. The default for the 'pdist' function, 'correlation', would include both the positive and negatives, but I'm interested in grouping inverse relationships as well. Does anyone know how I can achieve this?
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 23, 2026
There are two ways in which this can be done:
First, notice that 'pdist' computes one minus the correlations among rows:
>> x
x =
1 2 3 4
2 3 2 3
1 2 3 4
4 3 2 1
>> pdist(x,'cor')
ans =
0.5528 0 2.0000 0.5528 1.4472 2.0000
>> 1-corr(x')
ans =
0 0.5528 0 2.0000
0.5528 0 0.5528 1.4472
0 0.5528 0 2.0000
2.0000 1.4472 2.0000 0
1) The first way is to compute the distance as one minus the absolute correlation, and compute linkage based on that.
>> D = pdist(x,'cor');
>> linkage(D,'single')
ans =
1.0000 3.0000 0
2.0000 5.0000 0.5528
4.0000 6.0000 1.4472
>> C = 1-D % get correlation
C =
0.4472 1.0000 -1.0000 0.4472 -0.4472 -1.0000
>> D = 1-abs(C) % get 1-abs(correlation)
D =
0.5528 0 0 0.5528 0.5528 0
>> linkage(D,'single') % cluster using that
ans =
3.0000 4.0000 0
1.0000 5.0000 0
2.0000 6.0000 0.5528
Notice that points 1,3,4 are clustered with zero distance, even though the correlation with the point 4 is '-1'.
2) The second way is to write the distance into the 'linkage' command:
>> linkage(x,'single',@(xrow,ymat) 1-abs(corr(xrow',ymat')))
ans =
3.0000 4.0000 0
1.0000 5.0000 0
2.0000 6.0000 0.5528
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related AI Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →