-2

Say I have a vector: t=-10:0.01:9.99; which has a different vector length than a vector y. How do I find the indices of y that correspond to t?

wing155
  • 1
  • 2
  • Pad the shorter one with zeros, then y==t? Or did i misunderstand your question? – Prostagma May 03 '18 at 14:53
  • 2
    Please show a [mcve], the answer depends highly on `y` – Ander Biguri May 03 '18 at 15:00
  • 2
    This is probably an [XY-Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) since you [don't generally want to compare floating point values for equality](https://stackoverflow.com/questions/9508518). If you post a [mcve] then maybe you could get a helpful answer. – jodag May 03 '18 at 15:28

3 Answers3

3

You can use the ismember function to find the indices which match indices in another vector. E.g. something like this

x = 0:0.1:10; %some x data
y = x.^2; %some y data
xcoarse = 0:10; %coarser or restricted x data 
idx = ismember(x,xcoarse); %find indexes
yy = y(idx); %Find corresponding y values
Nicky Mattsson
  • 3,052
  • 12
  • 28
0

Here a simple example that does this:

a = -10:10;
b = -2:5;

idx = zeros(size(b));
for i = 1:length(b)
    idx(i) = find(a==b(i));
end

output:

idx =

     9    10    11    12    13    14    15    16

>> a

a =

   -10    -9    -8    -7    -6    -5    -4    -3    -2    -1     0     1     2     3     4     5     6     7     8     9    10

>> b

b =

    -2    -1     0     1     2     3     4     5

You can see that idx match the index of a where b is equal.

TwistedSim
  • 1,960
  • 9
  • 23
0

try this:

if t is [1x1999] elements and y is [1x100]

y[j] corresponds to t[i] such that j = unit64( (i/len(t) * len(y) ) this will make sure your y[1] and t[1] as well as y[end] and t[end] correspond, however you will still have significant loss of data but preserve the ratio. This is better than padding with zero as that can mess with the result.

However it is always better to work with vectors of equal length for a 1:1 correspondence. But if you don't mind the accuracy loss the above should work.

Hadi Farah
  • 1,091
  • 2
  • 9
  • 27
  • From what I understand, you have variables stored in 2 arrays which need to interact such as multiplication of elements or whatever but the arrays are not same length and so you cannot use the same index to correspond one array to another. – Hadi Farah May 03 '18 at 15:08
  • I am trying to make a plot where t is the time vector. However the vector lengths are not equal and I am trying to use the find function to find the indices that correspond to it – wing155 May 03 '18 at 15:11
  • To plot you need to make the two arrays equal, I suggest you try something similar to Nicky Mattsson's answer. – Hadi Farah May 03 '18 at 15:14
  • If your y and t and independent (no operation to link them) as in Nicky's example. Make a dummy array T[j] = t[ unit64( (j / len(y)) * len(t)]. Again this leads to a loss of info but should give you equal sized array to plot. This is a horrible method to do things by programming standards, but in engineering sometimes you can get away with it. You may need to round up instead of converting to int so you wouldn't suffer from array of index 0 in this case. – Hadi Farah May 03 '18 at 15:22