How to calculate distance between array elements?
Show older comments
Hi, Let say I have this array:
A = [1 3 5 1 4 1 2 6 1]
the distances between elements are:
1-3-5-1:
1 to 3 = 2
3 to 5 = 1
5 to 1 = 3
1-4-1:
1 to 4 = 2
4 to 1 = 1
1-2-6-1:
1 to 2 = 3
2 to 6 = 1
6 to 1 = 2
what I need is to accumulate the distance of elements between every two ones, and if the distance between every two ones is greater than 5 then a penalty of 3 should be added to the total distance of the whole array. For example the distance between 1-3-5-1 = 6, so in this case, a penalty of 3 should be added to the total distance and so on.
5 Comments
Guillaume
on 9 Apr 2018
1-3 = 2
Ok... I'd say it's -2 but let's not worry about the sign
3-5 = 1
5-1 = 3
Huh? What sort of math is that?
Hajem Daham
on 9 Apr 2018
Hajem Daham
on 9 Apr 2018
Guillaume
on 9 Apr 2018
I'm not any clearer. How is the distance from 3 to 5 not equal to 2? And from 5 to 1 not equal to 4?
Hajem Daham
on 9 Apr 2018
Accepted Answer
More Answers (0)
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!