From: Bruno Luong on 6 May 2010 14:36 > > Can you give an explanation for why Matlab shouldn't assume that a double array containing only 0s and 1s is intended as a logical index? It would encourage bad style, but as far as I can see it would be totally unambiguous. % Well if a = [1 2 3] % What would be a([1 1 1])? a(logical([1 1 1])) a(double([1 1 1])) % Bruno
From: Alan B on 6 May 2010 14:40 "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hrv26k$q4e$1(a)fred.mathworks.com>... > > > > > Can you give an explanation for why Matlab shouldn't assume that a double array containing only 0s and 1s is intended as a logical index? It would encourage bad style, but as far as I can see it would be totally unambiguous. > > % Well if > > a = [1 2 3] > > % What would be a([1 1 1])? > > a(logical([1 1 1])) > a(double([1 1 1])) > > % Bruno Right... but that doesn't address my question. Your index array has only 1s. My question concerns the following situation: >>unique(indexArray) [0 1] Why doesn't Matlab interpret such an array as logical? It would encourage the use of double arrays as logical, which would cause confusion when, for example, a double array suddenly becomes all ones. But why not interpret it as logical, and produce a warning, rather than just producing an error? I myself have no problem with explicitly converting a double array to logical, I'm just curious what the reasoning is behind this behavior.
From: Sergei Koulayev on 6 May 2010 14:52 "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hruq21$nef$1(a)fred.mathworks.com>... ...... > For this reason Mathworks had no choice but to eventually make this distinction between the two classes, logical and numerical. In the early version of matlab (4b) which I have, the distinction was *not* made and it led to much trouble for me in avoiding this pitfall. > > Roger Stafford Roger, I think yours is one of the best answers. The answers of the sort "Matlab does it like this therefore it should be like this period" are not really inspiring. Still, if you read all of the answers, they are about the same thing: how logicalness of an array can be important for the indexing reasons. While they are informative, still they sort of miss the grain of my example - and the reason for my outrage: when I have logical array on the LHS of the equation, Matlab did something quite unintuitive - it tried to preserve the logical nature of an array, even at the expense of misleading me! Really, when I write a(a==0) = 2 - I expect to have 2's at the place of zeros. I understand logicalness is an important thing, but having correct computations that meet user's expectations is the need number one.
From: Matt J on 6 May 2010 14:53 "Alan B" <monguin61REM(a)OVETHIS.yahoo.com> wrote in message <hrv2en$d9t$1(a)fred.mathworks.com>... > Right... but that doesn't address my question. Your index array has only 1s. My question concerns the following situation: > > >>unique(indexArray) > [0 1] > > Why doesn't Matlab interpret such an array as logical? ============== You're suggesting that indexArray should give one kind of behavior if it contains only 1s and another kind of behavior if it contains 0s as well? That would at minimum require additional overhead and pre-checking for the presence of zeros. Aside from that, if indexArray is generated programmatically, you wouldn't know in advance what the composition of 1s and 0s in indexArray will be. You would therefore have to write additional code everytime you wanted to process indexArray: one block to handle a mixture of 0s and 1s and one block to handle the special case where only 1s are present.
From: Sergei Koulayev on 6 May 2010 15:01
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hrv36g$30g$1(a)fred.mathworks.com>... > "Alan B" <monguin61REM(a)OVETHIS.yahoo.com> wrote in message <hrv2en$d9t$1(a)fred.mathworks.com>... > > > Right... but that doesn't address my question. Your index array has only 1s. My question concerns the following situation: > > > > >>unique(indexArray) > > [0 1] > > > > Why doesn't Matlab interpret such an array as logical? > ============== > > You're suggesting that indexArray should give one kind of behavior if it contains only 1s and another kind of behavior if it contains 0s as well? > > That would at minimum require additional overhead and pre-checking for the presence of zeros. > > Aside from that, if indexArray is generated programmatically, you wouldn't know in advance what the composition of 1s and 0s in indexArray will be. You would therefore have to write additional code everytime you wanted to process indexArray: one block to handle a mixture of 0s and 1s and one block to handle the special case where only 1s are present. Matt, what would you say about my last post? Do you agree that there should be at least a warning when someone tries to write double into logical? Can you think of any reasons of why preserving logicalness takes precedent? Even if I try to use that array for indexing later, I already know that its not logical, b/c it contains '2'. Also, if it has zeros, Matlab would give me an error, and that's fine. They should do something about this. |