Assuming M only contains zeros and ones (or you could clamp it to these values), you could use it as a mask as seen here:
# setup
b, n, d = 2, 10, 4
A = torch.randn(b, n, d)
M = torch.randint(0, 2, (b, n))

# your approach
B = []
C = []
for i in range(M.shape[0]):
    b = []
    c = []
    for j in range(M.shape[1]):
        if M[i,j] == 1:
            c += [A[i,j]]
        else:
            b += [A[i,j]]
    B += [torch.stack(b, dim=0)]
    C += [torch.stack(c, dim=0)]

B = torch.cat(B)
C = torch.cat(C)

# alternative
B_ = A[~M.bool()]
C_ = A[M.bool()]

print((B == B_).all())
> tensor(True)
print((C == C_).all())
> tensor(True)