From: Bruno Luong on 6 May 2010 15:03 > Why doesn't Matlab interpret such an array as logical? Simply because there is no reason for an algorithm to make any pre assumption about the content of the array. Assume we have the code: a = rand(1,2); b = double(a>0.5) c = a(b) Do you want the content of c to be? There are 1/4 chance that b does not contains any 0. So sometime (~1/4) it will return duplicated a([1 1]) The rest of the time (~3/4) it returns a(a>0.5); Do you prefer the above code behave in a random manner with a? That's how a bad code is built: ill-defined thus unpredicted behavior. Bruno
From: Bruno Luong on 6 May 2010 15:10 "Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message <hrv34k$sic$1(a)fred.mathworks.com>... > I understand logicalness is an important thing, but having correct computations that meet user's expectations is the need number one. No *consistent* behavior is the absolute priority. User expectation comes later, much later. There should be no room for interpretation in programming. Bruno
From: Roger Stafford on 6 May 2010 15:10 "Alan B" <monguin61REM(a)OVETHIS.yahoo.com> wrote in message <hrv1e8$63j$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. - - - - - - - - Alan, you are really missing the point here! Think carefully about what we have said. As I know all too well to my sorrow, when a program generates either numerical indices or logical values, there may be no way in advance to know if you might happen to generate, on the one hand all 1's, or on the other hand all true values. In such a case, which can happen all too frequently, there would be no way for matlab to distinguish between the two meanings unless there is a firm understanding about the difference between the two types of variables. In my own ancient system where no such distinction was made, the system makes the logical interpretation in case of such ambiguity. That means numerical indexing schemes that otherwise might work very well will often bomb out in such cases, and that in my opinion is totally unacceptable. Even if Mathworks had decided on the opposite interpretation, it would be logical indexing that would sometimes get screwed up. The change they made was absolutely necessary. Roger Stafford
From: Steven Lord on 6 May 2010 15:11 "Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message news:hrv34k$sic$1(a)fred.mathworks.com... > "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. So let's take another example, and because logical has only a small number of values it can take on I'm switching to uint8. The principles are the same, though. x = uint8(1:10); x(x==5) = pi What should the contents and data type of x be? Remember, pi is not an integer value and so CANNOT be exactly represented as a uint8. Should we automatically blow x up into the data type of pi (aka double)? Or should we convert pi into uint8 (with the result being a uint8 with the value 3) and the resulting x remains uint8? Now how about this example: x = uint8(1:10); x(x==5) = 1; What should the contents and data type of x be? Should we automatically blow x up into the data type of 1 (aka double)? Or should we convert 1 into uint8 and have x remain uint8? Is your answer to this question consistent with your answer above? One more example, and the question is the same -- can you tell me what the contents and data type of x should be after executing this code? y = input('Enter a scalar value. '); x = uint8(1:10); x(x==5) = y; "The data type depends on what the user entered for y" is not, IMO, a good answer. When performing subscripted assignment with parentheses like this, here's the rule. If the left-hand side (LHS) is not the same data type as the right-hand side (RHS) then the RHS data is converted into the type of LHS and then the converted data is inserted into LHS. I think that's a very consistent rule (and so I estimate about 30 seconds after I post, someone will remind me of a case I've forgotten where it doesn't hold true. "Subscripting-as-evaluation", like function handles, doesn't count, and neither does the case where a class author specifically and explicitly overloads subscripted assignment.) I don't want to have the special case "unless RHS contains values that don't 'fit' in which case we convert everything to the class of RHS." at the end of that rule unless it's absolutely necessary, and IMO it's not. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Alan B on 6 May 2010 15:23
"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, Bruno, Roger, I don't think my question is so outrageous... I remember an example of a standard Matlab function that computes some statistical metric on arrays columnwise, except when the input is a 3x3. In this case, it reverts to row-wise, or maybe does some 2-dimensional calculation. Unfortunately I do not remember the name of the function, and I may not be recalling the details correctly, but the point is that Matlab is not necessarily 100% consistent. Does anyone else know what I'm talking about? I'm not arguing FOR inconsistency, just observing that, if I recall correctly, Matlab doesn't have a perfect track record. |