Sorting 在数据列中查找接近数
我有3列数据(一天中的小时数) C1 C2 C3 01 05 00 05 09 06 11 11 10 161714 20 22 18Sorting 在数据列中查找接近数,sorting,Sorting,我有3列数据(一天中的小时数) C1 C2 C3 01 05 00 05 09 06 11 11 10 161714 20 22 18 我需要能够将其分离成一个n乘3的矩阵,其中每行上的三个数字彼此相隔+/-2小时。(每一行的范围必须是看起来,虽然你说的是有三列值,但它们的行和列没有任何意义。实际上,你只有一个数字列表:01、05、00、05、09、06、11、11、10、16、17、14、20、22、18 然后你点:00,01,05,05,06,09,10,11,11,14,16,17,18,
我需要能够将其分离成一个n乘3的矩阵,其中每行上的三个数字彼此相隔+/-2小时。(每一行的范围必须是看起来,虽然你说的是有三列值,但它们的行和列没有任何意义。实际上,你只有一个数字列表:01、05、00、05、09、06、11、11、10、16、17、14、20、22、18 然后你点:00,01,05,05,06,09,10,11,11,14,16,17,18,20,22 然后你取三个,看看它们的距离:00,01,05=糟糕,因为01和05的距离太远了。 下一个数字。01,05,05?否。下一个数字。05,05,06?是。在他们之后继续。09,10,11?是。在他们之后继续。11,14,16?否。下一个数字。14,16,17?是。您找到了一个解决方案:
05, 05, 06
09, 10, 11
14, 16, 17
我设法让它在MATLAB中几乎可以工作,我不认为它特别有效或聪明,但它确实做到了 但是,它很大程度上依赖于第一列中的值,将其视为中点 这意味着第2列和第3列中允许的点必须是
+/-2
如果C1中的值是一个下限(例如30)
,并且其他两个值最多比C1中的值大4,则被归类为无效,尽管从技术上讲这仍然是一个解决方案
x = [1,5,0; 5,9,6; 11,11,10; 16,17,14; 20,22,18];
d=size(x); %Get the size in of x in the form [row,col]
rows=d(1); %Number Of Rows
cols=d(2); %Number Of Columns
clear d;
y = nan(rows,cols); %nan Matrix the same size as x used for the output
a = ones(1,cols); %Keep track of the current index in question in each column
c = 0; %Number of "matches" or rows that are valid in the output matrix
time = zeros(1,cols); %Keep track of the current values in each column
while(max(a)<rows+1) %For every row check that no index is invalid
time(1)=x(a(1),1); %Get the value in column1
b = 2; %column counter
skip=0; %Increment the counter for column 1 if this is true
while(b<cols+1&&~skip&&max(a)<rows+1) %For columns 2->cols, if we don't need to skip the value in column 1 and all the indexes are valid.
time(b)=x(a(b),b); %Get the value in column b at row a(b)
delta = time(b)-time(1); %work out the difference in value from the first column value that is selected
if(delta>2)
%Skip first column by 1
a(1)=a(1)+1; %Increment the counter for column 1
skip=1; %Return back to the first while loop
elseif(delta<-2)
%Skip b'th column by 1
a(b)=a(b)+1; %Increment the counter for column b
else
%Its valid
if(b==cols) %If at the last column and its valid
c=c+1; %Increment the match counter
y(c,:)=time(1:cols); %Set the c'th row of the output to what we've found
a=a+1; %Move onto next number in column 1
skip=1; %Start all over
else %Not at last column yet
b=b+1;
end
end
end
end
x=[1,5,0;5,9,6;11,11,10;16,17,14;20,22,18];
d=大小(x);%以[行,列]的形式获取x的大小
行数=d(1);%行数
cols=d(2);%列数
清除d;
y=nan(行,列);%nan矩阵大小与用于输出的x相同
a=一(1,cols);%在每列中跟踪当前有问题的索引
c=0;输出矩阵中有效的“匹配项”或行数的百分比
时间=零(1,cols);%跟踪每列中的当前值
while(max(a)它们所在的列很重要,C1中的数据必须保留在矩阵的第一列中,同样,C2和C3中的数据需要保留在最终矩阵的第二列和第三列中。但它们所在的行并不重要。该列指的是在设定位置的测量值,因此C1、C2、C3是随时间不同的记录t位置,这就是为什么我需要保留它们所在列的知识。我试图做的是在2小时的时间内将所有数据从3个探测器收集到一行,并说这是“大致相同的时间”所以我可以比较不同的检测器。我希望这能澄清一点。@Thorsten在你的例子中,比如说:C1=[0,1,2],C2=[4,5,6],C3=[9,10,11],你的解决方案不起作用。列索引很重要,现在我看到了我的错误。对不起,我要再考虑一下。是的,谢谢,我知道了;-)我意识到我的错误。只是,我还没有看到一个好的解决方案。人们可以在列中的所有数字排列中循环,寻找好的行。这很简单,但根本不是所谓的聪明算法。我不知何故被困在那里。我会回来的,以防我有更好的想法。。。