How to align two different sets of data whilst allowing a +- 10% range?

2 views (last 30 days)
Hi,
I am struggling to match two large sets of data (a table and a matrix) whilst allowing a + -range of 10% in the matching process. Small examples are given below.
The first table has 3 columns:
1-RP 2-DURATION 3-MAG
1 30 19.0960000000000
1 60 12.8000000000000
1 120 7.92000000000000
2 30 24.6400000000000
2 60 16.2000000000000
5 15 51.9100000000000
5 30 31.7200000000000
----etc
The 2nd matrix has 2 columns with actual data, eg:
1-DURATION 2-MAG
125 7.46875000000000
5 0.0312500000000000
30 0.218750000000000
0 0
65 16.0312500000000000
30 31.0312500000000000
My desired output would consist of merging the matrix and table by matching both the duration and mag values (whilst allowing a +-10% range in the matching process). The output is to create a 3rd and 4th column in the 2nd matrix shown above containing 3-RP 4-DURATION from the original table in the corresponding row. If no match is found, a return of the number zero is required.
Like this:
125 7.46875000000000 1 120
5 0.0312500000000000 0 0
30 0.218750000000000 0 0
0 0 0 0
65 16.0312500000000000 2 60
30 31.0312500000000000 5 30
I have tried various functions such as the stacking function but I am struggling to find a solution that works for a large data set and does what I need.
Any help or advise would be very appreciated.
Thanks!

Accepted Answer

Sergey Kasyanov
Sergey Kasyanov on 9 Apr 2018
Hi))
%converting and renaming of variables
t=table2array(table1);
m=matrix2;
%0.1 - range of choosing
d=1+0.1*[1,-1];
for i=1:size(m,1)
for j=1:size(t,1)
%that may be reciprocal k
% 1./k
k=[m(i,1)/t(j,2)
m(i,2)/t(j,3)];
if prod((k<d(1)).*(k>d(2)))
m(i,3:4)=[t(j,1),t(j,2)];
end
end
end
  3 Comments
Sergey Kasyanov
Sergey Kasyanov on 10 Apr 2018
Yes.
k=[m(i,1)/t(j,2)
m(i,2)/t(j,3)];
k is a ratio between DURATIONS and MAGNITUDES accordingly k(1) and k(2).
(k<d(1)).*(k>d(2))
That is checking of values of ratios k both for upper and lower bounds. Operand .* is analog of logical AND.
prod((k<d(1)).*(k>d(2)))
That is analog of logical AND for values which are comprised in array.
if prod((k<d(1)).*(k>d(2)))
m(i,3:4)=[t(j,1),t(j,2)];
Save data if it is satisfying conditions.
ASJ
ASJ on 10 Apr 2018
Got it, thank you so much for taking the time to help and explain! :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!