- Concatenate all the vectors into one column vector.
- Sort it.
- Find indexes where the difference is smaller than 3 (some threshold).
- Find the corresponding indexes in the original vector.
Approximate matching of numbers across 20 Matrices
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
michio
2016-9-28
If all the 20 datenum vectors has the same length, the following (though not straight forward) could work.
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 个)
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!