Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Coordinating Zero Removals from Multiple Arrays

I've fielded some questions recently about how to coordinate multiple arrays changing simultaneously. One example is removing elements for two arrays in the case where either array holds a zero for the location. This is a good opportunity to reiterate the use of logical arrays and some useful associated functions (such as any and all).

Contents

Identify Pairs to Remove

Let's say I have 2 arrays

a = [ 1  4  9  0 25  0 49  0]
b = [ 1  0  3  0  0  6  7  8]
a =
     1     4     9     0    25     0    49     0
b =
     1     0     3     0     0     6     7     8

and I would like to delete the corresponding elements in a and b when either of them contains a zero value.

First Algorithm

There are several possible algorithms, each with their own trade-offs. Here's the first one.

anyzero = any([a;b] == 0)
a(anyzero) = []
b(anyzero) = []
anyzero =
     0     1     0     1     1     1     0     1
a =
     1     9    49
b =
     1     3     7

This algorithm combines the two arrays into one, a potentially costly move if the arrays are large. Then check for values that equal zero. And finally, check columnwise, using the function any, to identify the columns that have at least one zero. Finally, use this array of logical indices to delete the appropriate elements of a and b.

Second Algorithm

This algorithm (courtesy of Mirek L. in this post doesn't suffer from combining the two arrays.

x1 = a(a.*b ~= 0)
y1 = b(a.*b ~= 0)
x1 =
     1     9    49
y1 =
     1     3     7

But it calculates the same temporary array twice (and it's the size of one of the vectors). To be able to recalculate the temporary array this way, I can't overwrite the initial arrays as you see in the first algorithm. And finally, is there is a NaN or Inf corresponding to a 0, this algorithm won't find it.

Always Tradeoffs

There are always tradeoffs to make like the ones I mention here, at least when I program. How do you choose which tradeoffs to make? Which one would you choose here? Or would you choose an entirely different algorithm (which I hope you'll post). Let us know here.




Published with MATLAB® 7.9


  • print