Recently a customer asked me an interesting question about a good way to compute something. Before I sat down to tackle it, I happened to mention the problem to my pal, Sean. And he quickly answered, "max logical". It took me just a moment to realize what he was saying, and it is the answer to the problem. But when I heard it, I heard a person's name "Max Logical", and though I didn't know who that was, I immediately thought of Maxwell Smart, from the 60s tv comedy in the US, called "Get Smart".
By General Artists Corporation-GAC-management. [Public domain], via Wikimedia Commons
And you can see the precursor to today's mobile phones!
So what's the problem? The customer posed it by way of an example. Given a 3-D array A, generated for this example from a matrix, find the first plane in which some condition is true.
A=[1 2; 3 4]
M = cat(3, A, A+1, A+2, A+3);
So the function should work like this:
IDX = someFunction(M >=5);
IDX = [NaN 4; 3 2]
And, the customer wanted this to work for arrays of higher dimensions as well.
As I said, I mentioned this to Sean. But I had thought for about 2 seconds about it before and I realized I could do a find on the entire array, as if it were a vector, get the locations and sort them into their proper locations and find the first one in the correct last dimension. And then Sean said "Max Logical". Here's what a great solution looks like.
Why/how did this work? Well, I use the condition I want met into function max, and it is logical input, meaning only 0s and 1s. And max returns the index where the first condition is met, and I do this along the final dimension. Then create an array of NaN values, and use logical indexing to update this index matrix. Voila!
Now let's see if it makes sense in higher dimensions. For simplicity, I again will provide a base array in 1 dimension less than I am ultimately looking, so I can construct something for which I can reasonably easily check the result.
A = randi(17, [3 2 1 3]);
catdim = ndims(A)+1
M = cat(catdim, A, A+1, A+2, A+3, A+4);
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.