2
$\begingroup$

In a loop, I want to merge two matrices A and B where B is sometime empty.

Example:

A = {{1, 2, 3}, {4, 3, 2}}; B = {{}};
Join[A, B]

gives

{{1, 2, 3}, {4, 3, 2}, {}}

but I want

{{1, 2, 3}, {4, 3, 2}}

However,

A = {{1, 2, 3}, {4, 3, 2}}; B = {{2, 0, 1}};
Join[A, B]

should still give

{{1, 2, 3}, {4, 3, 2}, {2, 0, 1}}

How can I obtain A as the result in the first case with {} Curly Braces without using If or any complicated test.


I have to mention the level of merge Join[A,B,2]

$\endgroup$
3
$\begingroup$

Try

DeleteCases[Join[A, B], {}]

or

Join[A, B] /. {} -> Nothing

The latter requires V10.2 or later.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.