Spearman correlation in Matlab!
110 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Wayne King
am 27 Okt. 2011
Hi, If you have the Statistics Toolbox.
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');
0 Kommentare
Weitere Antworten (2)
Rithy Khouy
am 25 Aug. 2022
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');
0 Kommentare
DEVANSHI
am 6 Mär. 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
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!