0
votes

I have two arrays of dates, A and B, where size(A) > size(B).

A contains multiple entries for each date (it corresponds to a cell containing various data). B is simply all dates between the start and end date, but also correspond to certain data.

I want to create and array, C (where size(C) == size(A), containing the row number in B corresponding to the date on each row in A (so that data can be cross-referenced, i.e. perform a calculation based on data in A and B, using the row index of B to match dates).

I can do this using a loop and the find function:

for i=length(A)
C(i) = find(A(i) == B);
end

However, this is probably not the most efficient solution (takes quite a long time given my large data set). I'd "prefer" simply C = find(A == B), but Matlab does not allow this.

Is there a way to achieve the same result not using a loop?

Many thanks for any help!

3
Do you want C to be a cell array? - Eitan T
I want C to be a numerical array containing row indices corresponding to B. (But it doesn't matter as I can always convert it). Thank you for pointing out the accept function, I wasn't aware of it, will begin using it! - beerskij
C can be a 1-D array only if B contains unique values (i.e no repeating values). Are you okay with that? - Eitan T

3 Answers

1
votes

You can use second output argument of ismember, like so:

[tf, C] = ismember(A, B);
0
votes

You can use the second output of histc for this

# Put some dates in B
B = datenum('01-Jan-2013') : datenum('05-Jan-2013');

# Make A into a superset of B
A = B(randi(length(B), 1, 20));

# The vector C holds the indexes you want
[tmp, C] = histc(A,B);
0
votes

The function you need is intersect. Use it like this:

[a,b,C] = intersect(A,B)

While the results for a and b does not matter. The result for C is the C that you want