Reputation: 669
I have two vectors A
and B
containing date numbers. In theory, all coefficients in B
should also be contained in A
. Unfortunately this is not the case. My theory justifies replacing all non-corresponding date numbers with the next date number in the A
vector, for which I would like to write a script.
More concretely: Both vectors contain dates of financial transactions. Vector A
contains all trading days of the time period examined so that all of the transaction dates in B
should also be contained in A
because transactions cannot take place on non-trading days. Where an entry in B
is not in A
I hypothesise that trades were mistakenly reported to have taken place on a non-trading day and I would thus like to replace these dates with the next highest number that is contained in A
.
To provide an example, I would like to turn this:
A = [2;3;4;7]
B = [2;3;4;5;6;7]
Into this:
A = [2;3;4;7]
B = [2;3;4;7;7;7]
So that B(4:5)
were modified because they are not contained in A
. The script would have to identify that 5
is not contained in A
and it would then check whether 5+i
is contained in A
until this evaluates to true (which is the case at 7
). At this point the coefficient would be inserted into B
to replace the old date.
Note that this question is closely related to this earlier question; however it is not identifying business days that this question is about. Instead I want to accept one of my date vectors (A
) as authoritative (i.e. adequately representing trading days including national holidays) and base the substitution of the non-conforming entries on the A
vector instead of a Bloomberg function.
Upvotes: 2
Views: 37
Reputation: 112699
You can apply bsxfun
to compare each element of A
with each element of B
, and then use the second output of max
to find the required indices:
[~, ind] = max(bsxfun(@le, B(:).', A(:)), [], 1);
B_result = A(ind);
This assumes that for each element of B
there is at least one element in A
which is greater or equal. Values need not be integer or sorted. For example,
A = [2; 3.5; 4.5; 7; 10];
B = [0.1; 2; 2; 6; 5; 4.5];
gives
B_result =
2.0000
2.0000
2.0000
7.0000
7.0000
4.5000
Upvotes: 1
Reputation: 1051
This solution assumes that a
and b
are already sorted. If not, then simply replace them with sort(a)
and sort(b)
.
a = [ 2; 3; 4; 7 ];
b = [ 2; 3; 4; 5; 6; 7 ];
% Check each element in b
for k = 1: length(b)
% If this element of b is not contained in a
if (all(a(:) ~= b(k)))
% Replace with the next element in a that is greater than b(k)
b(k) = a(find(a(:) > b(k), 1, 'first'));
end
end
Upvotes: 2