indexs = linspace(1,d,d);%生成等差数列。语法为:linspace(x1, x2, n),其中x1是起始值,x2是终止值,n是生成的数字个数 flags = logical(indexs);%逻辑函数 x_e = sum((x-mean(x)).^2);%mean 矩阵均值 方差 sgc = []; g_h = 0; g_h_e = 0; while ~isempty(indexs(flags)) temp_index = indexs(flags); source = y(:,temp_index(1)); flags(temp_index(1)) = 0; temp_index = temp_index(2:end); temp_flag = []; for i = 1: length(temp_index) corrs = corrcoef(source,y(:,temp_index(i)));%矩阵相关系数 corrs = corrs(1,2); if corrs >= threshold_corr temp_flag(end+1) = i; end end flags(temp_index(temp_flag)) = 0; sgc(:,end+1) = source+sum(y(:,temp_index(temp_flag)),2); g_h = sum(sgc,2); g_h_e = sum((x-g_h).^2); if g_h_e / x_e < threshold_nmse break end将这块的相似条件改为余弦相似度
时间: 2023-06-16 17:03:57 浏览: 201
可以将代码中的corrcoef函数替换为pdist2函数,使用余弦相似度计算矩阵之间的相似度。具体代码如下:
indexs = linspace(1,d,d);
flags = logical(indexs);
x_e = sum((x-mean(x)).^2);
sgc = [];
g_h = 0;
g_h_e = 0;
while ~isempty(indexs(flags))
temp_index = indexs(flags);
source = y(:,temp_index(1));
flags(temp_index(1)) = 0;
temp_index = temp_index(2:end);
temp_flag = [];
for i = 1: length(temp_index)
corrs = pdist2(source', y(:,temp_index(i))', 'cosine');
if corrs <= threshold_cosine
temp_flag(end+1) = i;
end
end
flags(temp_index(temp_flag)) = 0;
sgc(:,end+1) = source+sum(y(:,temp_index(temp_flag)),2);
g_h = sum(sgc,2);
g_h_e = sum((x-g_h).^2);
if g_h_e / x_e < threshold_nmse
break
end
end
这样代码中的相似条件就被改为了余弦相似度。
阅读全文