Matching two array elements by rounding up and down
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dear mathworks community,
I need to create a conditional array that I find quite challenging. I uploaded an example data list and result that shows what I need to have at the end as both Excel file and txt file.
I have a reference list that is a column vector. It contains numbers from -10 to 10 with step size of 1 as in the uploaded file and I have a secondary list that contains numbers from -10 to 10 again, but the step sizes differ and list B can be shorter than list A. What I need to do is this:
I need to match the elements of two lists by rounding up or down the elements of list B and the below condition should apply:
if |listA(n) – listB(m)| < |listA(n) – listB(m+1)|
listA(n) matches with listB(m).
So each member of listA matches with the closest element of listB.
Also, if there is no closer value of listB(m) to listA(n), then, listB(m) can be used multiple times.
For example,
listA(n) = -5, listA(n+1) = -6 ,
listB(m) = -4.6, listB(m+1) = -5.2, listB(m+2) = -5.8 and listB(m+3) = -6.5
listA(n) - listB(m) = |-5-(-4.6)| = 0.4
listA(n) - listB(m+1) = |-5 – (-5.2)| = 0.2
since 0.2 < 0.4 listA(n) matches with listB(m+1)
and listA(n+1) - listB(m+2) = |-6 – (-5.8)| = 0.2,
listA(n+1) - listB(m+3) = |-6-(-6.5)| = 0.5
so listA(n+1) matches with listB(m+2)
Note: The difference between the elements of list A and list B can be greater than 1, still the same rule applies.
Note2: This is not a homework. I just need to calculate rms of the difference between reference list and secondary list.
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!