我希望按如下方式处理数据。
EG。 说数据x(i)= [1 2 3 5 2 1]。 比较应该是元素 INDEX [1到2,1到3,1到4,1到5,1到6,2到3,2到4,2到5,2到6,3到4 ....] 遵循上述逻辑 因此,元素值距离 = [1,2,3,4,5,1,2,3,4,1,2,3,1,2,1]。
因此差异的元素值 = [1,2,4,1,0,1,3,0,1,2,1,2,3,4,1]。 我已经写了相同的以下代码,但我注意到我想要的最终矩阵'b'总是在它应该是常数时改变大小。我欢迎任何建议
clc;
close all;
clear all;
% read data set
I= imread('img2.bmp');
G=double(rgb2gray(I));
%choose 2 random numbers
n = 1;
s = [1 960];
k = round(rand(n,1)*range(s)+min(s));
for i = 1:length(k)
% choose a particular row from a matrix
row_no=k(i);
%G=R(row_no,:);
% compare every element with its neigbour to create distance and difference matrix
x1=row_no;
x2=row_no;
for y1 = 1:length(G)%size(G,2)
for y2 =1:length(G) %1:size(G,2)
distance_Mat(y1,y2) = round(sqrt((y2-y1)^2 + (x2-x1)^2));
difference_Mat(y1,y2) = 1*(G(x1,y1) - G(x2,y2))^2;
end
end
%% now remove repeating comparisons
b=horzcat(distance_Mat(:),(difference_Mat(:)));
[UniXY,Index]=unique(b,'rows');
DupIndex=setdiff(1:size(b,1),Index);
b(DupIndex,:)=[];
%calculate the cumulative sums and store it in different colums of data matrix
A1 = cumsum(b);
data(:,1)=A1;
end
答案 0 :(得分:2)
如果您有统计工具箱,那么
distance_Mat = squareform(pdist(x'));
只进行一次比较,然后镜像数据。
你可以得到下半部分tril(distance_Mat,-1);
如果您没有工具箱,请尝试以下操作:
I = tril(ones(numel(x)),-1);
[r,c] = find(I);
distance_Mat = zeros(numel(x));
distance_Mat(logical(I)) = round(sqrt((x(r)-x(c)).^2)