How can I mathematically determine the next available index for a child row in this data set?

41 Views Asked by At

I have a data set that looks like this:

$$ \begin{array}{c|lcr} & 0 & 1 & 2&3&4&5&6&7&8&9 \\ \hline parent & x & x & x & x & & & x & & & \\ child1 & & x & & x & x & & & & & \\ child2 & & x & x & & x & x & & & & \\ child3 & & & x & & & & x & & & \\ \end{array} $$

An $x$ represents a data index that is defined somewhere else.

I need to know, for any $childN$, which is the first index that is not defined at both the parent and $childN$ level. How can I do this mathematically or logically?

In the above example, $child1$'s next available index is 5, $child2$'s next available index is 7, and $child3$'s next available index is 4.

Essentially I want to build an algorithm to determine the first available index for $childN$, according to the condition that I listed above (parent must not be defined at that index).

1

There are 1 best solutions below

0
On

Let P and C be logical representations of Parent row and Child_N. You're looking for the min non zero index of !Parent & !Child. In MATLAB you can code it as follows

P=[1 1 1 1 0 0 1 0 0 0];

C=[0 1 0 1 1 0 0 0 0 0];

I=find(not(P) & not(C))

I = 6 8 9 10

min(I)

ans = 6

Note that array index starts with 1 in MATLAB, so 6 corresponds to your 5'th column index.