function Y = diffumap(B)
n = size(B,1);
% 1. 計算相似度與轉移矩陣
W = exp(-0.05 * squareform(pdist(B, 'squaredeuclidean')));
P = diag(1./sum(W,1)) * W;
% 2. 特徵值分解
[V, L] = eig(P);
L = diag(L); % 取出對角線上的特徵值變成一維向量
% 3. 強制取實數 (避免極小的浮點數運算虛數誤差)
V = real(V);
L = real(L);
% 4. 手動將特徵值與特徵向量由大到小排序
[L_sorted, idx] = sort(L, 'descend');
V_sorted = V(:, idx);
% 5. 計算降維後的座標 Y (特徵向量乘上特徵值)
% 注意:這裡改為取第 2 到第 4 個特徵向量與特徵值 (捨棄無意義的第 1 維)
Y = V_sorted(:, 2:4) * diag(L_sorted(2:4));
% 6. 3D 視覺化
figure;
hold on; grid on;
% 建立漸層顏色矩陣
colors = [1 - (1:n)'/n, repmat(0.8, n, 1), (1:n)'/n];
scatter3(Y(:,1), Y(:,2), Y(:,3), 12, colors, 'filled');
title('Diffusion Map 3D Projection');
xlabel('Component 1 (2nd Eigenvector)');
ylabel('Component 2 (3rd Eigenvector)');
zlabel('Component 3 (4th Eigenvector)');