Approximate matching of numbers across 20 Matrices

조회 수: 1 (최근 30일)
Tyler Smith
Tyler Smith 2016년 9월 27일
편집: Aristo 2017년 10월 31일
I have 20 matrices which house multiple columns of data. The first column is the datenum. I need to essentially look through all 20 matrices to find approximate datenum matches according to a threshold (threshold probably = 3 or 4). The threshold is needed due to a spatial lag between locations (each location is a different matrix with different dates). Example:
A=(712549, 712658, 715188, 715935, 716613, 716948, 717319, 717764, 718091, 718484),
B = (714401, 716219, 717321, 717716, 718506, 718802, 722427, 722853, 723546, 723554)
and C = (718802, 722427, 722853, 723546, 723554, 724633, 726821, 731624, 731958, 733730).
If a datenum in A is within 3 of a datenum in B and/or C, I want to identify that datenum as well as tag which matrices were matches and the corresponding datenum. An example output for one approx. match between matrix B and C would be: 723554 | B | 723555 | C (with the | symbol meaning separated by columns and the letter belonging to the datenum on its left). Any help would be great! Thanks.

채택된 답변

michio
michio 2016년 9월 28일
If all the 20 datenum vectors has the same length, the following (though not straight forward) could work.
  1. Concatenate all the vectors into one column vector.
  2. Sort it.
  3. Find indexes where the difference is smaller than 3 (some threshold).
  4. Find the corresponding indexes in the original vector.
A=[712549, 712658, 715188, 715935, 716613, 716948, 717319, 717764, 718091, 718484];
B =[714401, 716219, 717321, 717716, 718506, 718802, 722427, 722853, 723546, 723554];
C =[718802, 722427, 722853, 723546, 723554, 724633, 726821, 731624, 731958, 733730];
All = [A', B', C'];
% 1: Concatenate all the vectors into one column vector.
Alldata = All(:);
% 2: Sort it. (index refers to the original location in Alldata)
[sorted,index] = sort(Alldata);
% 3: Find indexes where the difference is smaller than 3 (some threshold).
idx_close = find(diff(sorted) < 3);
% 4: Find the corresponding indexes in the original vector.
a0 = Index(idx_close);
a1 = Index(idx_close+1);
[I0,J0] = ind2sub([10,3],a0); % 10 : length of the datenum vector
[I1,J1] = ind2sub([10,3],a1); % 3 : number of variables (A, B, C)
% Now we know that All(I0,J0) is close to All(I1,J1). Let's display it.
variable_name = ['A','B','C'];
for ii=1:length(I0)
str0 = [variable_name(J0(ii)), ' ', num2str(All(I0(ii),J0(ii)))];
str1 = [variable_name(J1(ii)), ' ', num2str(All(I1(ii),J1(ii)))];
disp(['match : ', str0, ' and ', str1]);
end
  댓글 수: 1
Tyler Smith
Tyler Smith 2016년 9월 28일
Thanks for the help! Unfortunately all the matrices are different sizes. If I just use the following I can at least find matches and go back and determine when matrix it belonged to: % %
A=[712549, 712658, 715188, 715935, 716613, 716948, 717319, 717764, 718091, 718484]';
B =[714401, 716219, 717321, 717716, 718506, 718802, 722427, 722853, 723546, 723554]';
C =[718802, 722427, 722853, 723546, 723554, 724633, 726821, 731624, 731958, 733730]'; % Concatenate all the vectors into one column vector.
Alldata = vertcat(A,B,C);
% Sort it. (index refers to the original location in Alldata)
[sorted,Index] = sort(Alldata);
% Find indexes where the difference is smaller than 3 (some threshold).
idx_close = find(diff(sorted) < 3);
% Find the corresponding indexes in the original vector.
a0 = Index(idx_close);
a1 = Index(idx_close+1);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

José-Luis
José-Luis 2016년 9월 28일
편집: José-Luis 2016년 9월 28일
A = [712549, 712658, 715188, 715935, 716613, 716948, 717319, 717764, 718091, 718484];
B = [714401, 716219, 717321, 717716, 718506, 718802, 722427, 722853, 723546, 723554];
tol = 3;
[idxB, idxA] = ind2sub([numel(A),numel(B)],find(abs(bsxfun(@minus,A,B')) < tol));
  댓글 수: 3
José-Luis
José-Luis 2016년 9월 28일
You could solve that with a loop. Give it a shot and I'll be happy to help if you get stuck.
There are other alternatives as well.
Aristo
Aristo 2017년 10월 31일
편집: Aristo 2017년 10월 31일
How about for floating points and different size of matrix

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by