Approximate matching of numbers across 20 Matrices

3 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

michio
michio il 28 Set 2016
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 Commento
Tyler Smith
Tyler Smith il 28 Set 2016
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);

Accedi per commentare.

Più risposte (1)

José-Luis
José-Luis il 28 Set 2016
Modificato: José-Luis il 28 Set 2016
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 Commenti
José-Luis
José-Luis il 28 Set 2016
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 il 31 Ott 2017
Modificato: Aristo il 31 Ott 2017
How about for floating points and different size of matrix

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by