Combining Conditional Masks

Suppose we have some random distribution of integer values between 1 and 3.

A=torch.round(torch.rand(10000)*2)+1

And we want two masks combined in the following order:

cond1=A!=2.0

B=A[cond1]

cond2=B[:-1]!=B[1:]

all_but_firstB=B[1:]

C=torch.cat([B[0].view(-1), all_but_firstB[cond2]])

How would I go about combining the two masks as one to apply to the original vector A? Such that:

A[one_mask]=C

I was able to tackle this with using a vector range, applying the masks to that range, then applying that range as a mask to A. As follows:

data_size=10000
A=torch.round(torch.rand(data_size)*2)+1

b=torch.arange(data_size)

cond1=A!=2.0

b = b[cond1]

cond2 = A[cond1][:-1]!=A[cond1][1:]

all_but_firstb=b[1:]

b=torch.cat([b[0].view(-1), all_but_firstb[cond2]])

print(torch.all(A[b]==C))