finding the distance between elements of a vector

32 views (last 30 days)
I have lots of data looking something like this, but actually much larger:
[0 0 0 3 0 0 2 0 -3 0 5 0 -2 0 0 3 0 0 0 0 2 0 -5]
Now I want to be able to extract the index distance between any 2 numbers in the data.
For example say i want to know the distance between 3 and 2, it should give 3 and 5 as result. I'm not too good at Matlab and am not sure if this is possible and how it could be done.
Can anybody help?
  2 Comments
Pawel Blaszczyk
Pawel Blaszczyk on 30 Sep 2011
Can you explain more clearly?
Why the distance between 3 and 2 is 3 and 5?
The distance is a scalar, not a vector, isn't it?
Jan
Jan on 30 Sep 2011
This is not enough information to create a complete answer. What is the full output for [3, 2, 2], or for [0, 1, 2, 0]?

Sign in to comment.

Accepted Answer

Matt Tearle
Matt Tearle on 3 Oct 2011
OK, from what I can tell from your further input, I think this function does what you want.
function d = xyspace(A,x,y)
idx1 = find(ismember(A,x));
idx2 = find(ismember(A,y));
x = union(idx1,idx2);
whichset = ismember(x,idx1) + 2*ismember(x,idx2);
idx = diff(whichset)>0;
d = x([false,idx]) - x([idx,false]);
So now
A = [0 -2 -2 3 0 0 0 -2 -3 0 0 3 0 2 -3 -2 0 3 0 0 2 0 -3 0 -2 0 0 3 2 -3 -2 0 0 3 0];
xyspace(A,3,2)
ans =
2 3 1
I'm not sure what output you expect for the example
A = [2 0 0 3 0 -2 2 0 -3 0 5 3 0 -2 -5 0 0 0 0 0 0 2 0 5 0 3 0 5 0]
My function can do:
xyspace(A,3,[2,5])
ans =
3 10 2
and it gives the distance from any 3 to a following 2 or 5 (whichever comes first). Not sure if that's what you want. If you want both 3-2 and 3-5 distances, you can always just call twice.
  2 Comments
Bauke
Bauke on 5 Oct 2011
Thanks!
This seems to work splendidly!
I don't need to do what you explained with your second example, but I end up having the call the function twice :).
Matt Tearle
Matt Tearle on 5 Oct 2011
Great. If you don't need "x" and "y" to be vectors, you can replace the "ismember(A,x)" (or y) with "A == x" (y).

Sign in to comment.

More Answers (3)

Aurelien Queffurust
Aurelien Queffurust on 30 Sep 2011
A=[0 0 0 3 0 0 2 0 -3 0 5 0 -2 0 0 3 0 0 0 0 2 0 -5];
result = [find(A==2)- find(A==3)]
will return :
result =
3 5

Matt Tearle
Matt Tearle on 30 Sep 2011
Do you know that you'll always have pairs? That is, could you ever have a vector [0 2 0 -3 0 5 0 -2 0 0 3 0 0 0 0 2 0 -5] or [2 0 3 2 0 -3 0 5 0 -2 0 0 3 0 0 0 0 2 0 -5]? If so, Aurelien's solution will fail. This, sick and wrong though it may be, should work:
A = [0 0 0 3 0 0 2 0 -3 0 5 0 -2 0 0 3 0 0 0 0 2 0 -5];
xyspace = @(v,x,y) cellfun(@length,regexprep(regexp([' ',num2str(v),' '],[' ',num2str(x),' .*? ',num2str(y),' '],'match'),'\W',''))-1;
xyspace(A,3,2)
Note: use Aurelien's solution if you know the numbers will come in pairs!
  1 Comment
Bauke
Bauke on 3 Oct 2011
I've been playing around with your suggested answer, and it works, though I still don't understand it...
I have stumbled upon one problem; If your script runs into 3 which is not followed by a 2. This in theory shouldnt happen, but happens once in a while in my experimentally determined data. In that case your solution gives the distance between the 3 and the next 2.
For example:
A = [0 -2 -2 3 0 0 0 -2 -3 0 0 3 0 2 -3 -2 0 3 0 0 2 0 -3 0 -2 0 0 3 2 -3 -2 0 0 3 0]
your script returns:
10 3 1
But ideally it should return:
2 3 1
As the nearest 3 and 2 pair should be taken.
Seeing as i dont understand your answer yet the only thing i can think of is to throw away the first result in the example and use the rest as these are fine.
Thanks for your help and hopefully you can help me a bit further :)

Sign in to comment.


Bauke
Bauke on 1 Oct 2011
I should have explained it beter.
My data is mostly zeroes, but has several peaks of specific height.
Now, I want to know the distance between some of these specific peaks (Distance as in, the difference in index). However I really need the distance between a specific peak and the next specific peak, and not all of the peaks.
So I need to ignore the other peaks and only measure the distance with the upcoming peak.
In prinicple the data should come in pairs, but that is not the case with the experimentaly determined data :S..
So for example, If I have data like this:
[2 0 0 3 0 -2 2 0 -3 0 5 3 0 -2 -5 0 0 0 0 0 0 2 0 5 0 3 0 5 0]
I need to find the distance between the 3 and the next 2, and eventually also (if possible simultaneously) determine the distance between the 3 and the next 5.
It seems that Matt's answer works... Though I dont understand it yet.

Categories

Find more on MATLAB Mobile Fundamentals 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!