フィルターのクリア

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 Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by