Spearman correlation in Matlab!
Afficher commentaires plus anciens
Hey Matlab users,
If I have two series of data:
a = [1 4 6 3 4 6 7 8]; b [34 56 34 56 79 23 48 28];
and I want to perform a Spearman correlation what would be the proper command in MATLAB? I need both p-value and RHO.
Thanks for your help.
Mehdi
Réponse acceptée
Plus de réponses (2)
Rithy Khouy
le 25 Août 2022
0 votes
a = [1 4 6 3 4 6 7 8];
b = [34 56 34 56 79 23 48 28];
[RHO,PVAL] = corr(a',b','Type','Spearman');
DEVANSHI
le 6 Mar 2025
n = input("Enter the number of observations: ");
x = zeros(1, n);
y = zeros(1, n);
for i = 1:n x(i) = input("Enter the x: ");
y(i) = input("Enter the y: ");
end
[sx, idx_x] = sort(x);
rx = zeros(1, n);
AF_x = 0;
i = 1;
while i <= n j = i;
while j <= n && sx(j) == sx(i) j = j + 1;
end avg_rank = mean(i:j-1);
for k = i:j-1 rx(idx_x(k)) = avg_rank;
end
t = j - i;
if t > 1 AF_x = AF_x + (t^3 - t) / 12;
end
i = j;
end
[sy, idx_y] = sort(y);
ry = zeros(1, n);
AF_y = 0;
i = 1;
while i <= n j = i;
while j <= n && sy(j) == sy(i) j = j + 1;
end avg_rank = mean(i:j-1);
for k = i:j-1 ry(idx_y(k)) = avg_rank;
end
t = j - i; if t > 1 AF_y = AF_y + (t^3 - t) / 12;
end
i = j;
end d = rx - ry;
d2 = sum(d .^ 2);
spc = 1 - (6 * (d2 + AF_x + AF_y)) / (n * (n^2 - 1));
disp("Spearman's Rank Correlation Coefficient:");
disp(spc);
for j=1:n
if x(j)<x(i)
yrank=yrank+1
elseif y(j)==y(i)&& j==i
xcount=xcount+1
end
Catégories
En savoir plus sur Correlation and Convolution dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!