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 asanyandall).
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 inaandbwhen 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 functionany, to identify the columns that have at least one zero. Finally, use this array of logical indices to delete the appropriate elements ofaandb.
Second Algorithm
This algorithm (courtesy of Mirek L. inthis postdoesn'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 aNaNorInfcorresponding to a0, 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 doyou选择权衡make? Which one would you choose here? Or would you choose an entirely different algorithm (which I hope you'll post). Let us knowhere.
- Category:
- Indexing,
- Vectorization
Comments
To leave a comment, please clickhereto sign in to your MathWorks Account or create a new one.