# Thread Subject: What a hell is wrong with logical indexing??? Really???

 Subject: What a hell is wrong with logical indexing??? Really??? From: Sergei Koulayev Date: 6 May, 2010 14:22:05 Message: 1 of 40 People, check this out. I always thought I could treat 0-1 resulting from the logical operations as numbers. But apparently not. Why? It is completely unintuitive and dangerous. Can somebody give me a meaning of the following example? a = rand(100,1); i = a<0.5; i = i.^2; i(i==0) = 2; unique(i) ans =      1      2 i = a<0.5; i(i==0) = 2; unique(i) ans =      1
 Subject: What a hell is wrong with logical indexing??? Really??? From: Sean Date: 6 May, 2010 14:31:11 Message: 2 of 40 "Sergei Koulayev" wrote in message ... > People, > > check this out. I always thought I could treat 0-1 resulting from the logical operations as numbers. But apparently not. Why? It is completely unintuitive and dangerous. Can somebody give me a meaning of the following example? > > a = rand(100,1); > i = a<0.5; > i = i.^2; > i(i==0) = 2; > unique(i) > > ans = > > 1 > 2 > > i = a<0.5; > i(i==0) = 2; > unique(i) > > ans = > > 1 Try this: %%% a = rand(100,1); i = a<0.5; i = i.^2; i(i==0) = 2; class(i) unique(i) %%% a = rand(100,1); i = a<0.5; i(i==0) = 2; class(i) unique(i) %% Explanation 1st case: Squaring the i-vector turns it into a double so that values can be anything. 2nd case: The vector is logical so saying anything other than zero is true and it turns it to 1.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Steven Lord Date: 6 May, 2010 14:44:12 Message: 3 of 40 "Sergei Koulayev" wrote in message news:hrujad\$fmn\$1@fred.mathworks.com... > People, > > check this out. I always thought I could treat 0-1 resulting from the > logical operations as numbers. Of course. > But apparently not. Why? It is completely unintuitive and dangerous. Can > somebody give me a meaning of the following example? > > a = rand(100,1); > i = a<0.5; > i = i.^2; The above line converts i from being a _logical_ array into a _double_ array. [You can confirm this by looking at WHOS or by asking for its CLASS.] When you do so ... > i(i==0) = 2; this replaces all 0's in the double array with the double value 2. Therefore the array has two unique values, as your unique call indicated. > unique(i) > > ans = > > 1 > 2 > > i = a<0.5; > i(i==0) = 2; When you perform the above line, you're assigning the double value 2 into a _logical_ array. Therefore, the double value 2 needs to be converted into a logical value, and what is it converted into? >> x = logical(2) x =      1 The LOGICAL function converts 0 into false, errors if given a NaN or a complex value, and converts all other values (1, 2, pi, -73, Inf, 1.234567, etc.) into true. Therefore, when you perform the above operation the logical array i contains only true values, and so ... > unique(i) > > ans = > > 1 there is only one value in your logical array, true. This is the correct behavior. -- Steve Lord slord@mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
 Subject: What a hell is wrong with logical indexing??? Really??? From: Matt Fig Date: 6 May, 2010 14:48:04 Message: 4 of 40 Nothing seems out of sorts to me. In the first case you are assigning a double to a double. In the second case you are assigning a double to a logical, so MATLAB preserves the logical. Sort of like this: A = 3; A(2) = uint8(9); A(3) = 'r'; whos A Now as for the warning in your case, yet none in the above example, I question that.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Sergei Koulayev Date: 6 May, 2010 15:42:06 Message: 5 of 40 Hi Steve, thanks for the reply. I don't know about others, but I always cared about logical arrays as simply arrays that contain just 0 and 1's. It is quite disturbing that logical arrays can behave differently from arrays consisting of 0 and 1's (double). You explanation is about how things work in Matlab, and its good to know. But why Matlab should be so obsessed with preserving "logicalness" of the array - that I don't understand. Given than most users probably treat those arrays just like I do. Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's? Sergei > When you perform the above line, you're assigning the double value 2 into a > _logical_ array. Therefore, the double value 2 needs to be converted into a > logical value, and what is it converted into? > > >> x = logical(2) > > x = > > 1 > > The LOGICAL function converts 0 into false, errors if given a NaN or a > complex value, and converts all other values (1, 2, pi, -73, Inf, 1.234567, > etc.) into true. Therefore, when you perform the above operation the > logical array i contains only true values, and so ... > > > unique(i) > > > > ans = > > > > 1 > > there is only one value in your logical array, true. This is the correct > behavior. > > -- > Steve Lord > slord@mathworks.com > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ >
 Subject: What a hell is wrong with logical indexing??? Really??? From: Sean Date: 6 May, 2010 15:56:04 Message: 6 of 40 "Sergei Koulayev" wrote in message ... > Hi Steve, > > thanks for the reply. I don't know about others, but I always cared about logical arrays as simply arrays that contain just 0 and 1's. It is quite disturbing that logical arrays can behave differently from arrays consisting of 0 and 1's (double). You explanation is about how things work in Matlab, and its good to know. But why Matlab should be so obsessed with preserving "logicalness" of the array - that I don't understand. Given than most users probably treat those arrays just like I do. > > Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's? > Let's say it's a double: A = [4 5 6; 6 7 2]; idx = [1 1 0 0 1 1]; %double A(idx) is now going to try and reference A(0) which in MATLAB is undefined. It would also be referencing A(1) multiple times. This would be pretty useless but it's very important for other indexing. Such as: idx = [1 3 5]; A(idx);      ans = 4 5 6 I doubt anyone with much MATLAB experience has an issue with this functionality.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Mark Shore Date: 6 May, 2010 16:08:05 Message: 7 of 40 "Sergei Koulayev" wrote in message ... > > Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's? > > Sergei > Well, storage of only 1 byte per element rather than 8 bytes for one thing, and no way for floating point errors to mess things up for another.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Roger Stafford Date: 6 May, 2010 16:17:05 Message: 8 of 40 "Sergei Koulayev" wrote in message ... > Hi Steve, > > thanks for the reply. I don't know about others, but I always cared about logical arrays as simply arrays that contain just 0 and 1's. It is quite disturbing that logical arrays can behave differently from arrays consisting of 0 and 1's (double). You explanation is about how things work in Matlab, and its good to know. But why Matlab should be so obsessed with preserving "logicalness" of the array - that I don't understand. Given than most users probably treat those arrays just like I do. > > Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's? > > Sergei   Sergei, I'll give you an example of why the distinction needs to be made between logical variables and numbers that are 1's and 0's. Suppose that x is equal to the vector [5,7]. Then x([true,true]) would need to be [5,7] according to the rules of logical indexing, while x([1,1]) of necessity has to be equal to [5,5] according to the rules of numerical indexing.   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
 Subject: What a hell is wrong with logical indexing??? Really??? From: Alan B Date: 6 May, 2010 18:23:04 Message: 9 of 40 "Roger Stafford" wrote in message ... > "Sergei Koulayev" wrote in message ... > > Hi Steve, > > > > thanks for the reply. I don't know about others, but I always cared about logical arrays as simply arrays that contain just 0 and 1's. It is quite disturbing that logical arrays can behave differently from arrays consisting of 0 and 1's (double). You explanation is about how things work in Matlab, and its good to know. But why Matlab should be so obsessed with preserving "logicalness" of the array - that I don't understand. Given than most users probably treat those arrays just like I do. > > > > Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's? > > > > Sergei > > Sergei, I'll give you an example of why the distinction needs to be made between logical variables and numbers that are 1's and 0's. Suppose that x is equal to the vector [5,7]. Then x([true,true]) would need to be [5,7] according to the rules of logical indexing, while x([1,1]) of necessity has to be equal to [5,5] according to the rules of numerical indexing. > > 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 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.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Matt J Date: 6 May, 2010 18:32:04 Message: 10 of 40 > 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. =================== To see that it would be ambiguous, consider the following example and the different behavior that each gives >> a=10:10:50 %data a =     10 20 30 40 50 >> a([1 1 1 1]) %treats the 1s as doubles ans =     10 10 10 10 >> a(logical([1 1 1 1])) %treats the 1s as logicals ans =     10 20 30 40
 Subject: What a hell is wrong with logical indexing??? Really??? From: Bruno Luong Date: 6 May, 2010 18:36:04 Message: 11 of 40 > > 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
 Subject: What a hell is wrong with logical indexing??? Really??? From: Alan B Date: 6 May, 2010 18:40:23 Message: 12 of 40 "Bruno Luong" wrote in message ... > > > > > 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.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Sergei Koulayev Date: 6 May, 2010 18:52:04 Message: 13 of 40 "Roger Stafford" wrote in message ... ..... > 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.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Matt J Date: 6 May, 2010 18:53:04 Message: 14 of 40 "Alan B" wrote in message ... > 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.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Bruno Luong Date: 6 May, 2010 19:03:04 Message: 16 of 40 > 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
 Subject: What a hell is wrong with logical indexing??? Really??? From: Bruno Luong Date: 6 May, 2010 19:10:21 Message: 17 of 40 "Sergei Koulayev" wrote in message ... > 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
 Subject: What a hell is wrong with logical indexing??? Really??? From: Roger Stafford Date: 6 May, 2010 19:10:22 Message: 18 of 40 "Alan B" wrote in message ... > 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
 Subject: What a hell is wrong with logical indexing??? Really??? From: Alan B Date: 6 May, 2010 19:23:21 Message: 20 of 40 "Matt J " wrote in message ... > "Alan B" wrote in message ... > > > 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.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Matt J Date: 6 May, 2010 19:30:20 Message: 21 of 40 "Sergei Koulayev" wrote in message ... > 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? ============== No. You seem to think the user should be free to treat logicals as equivalent to double arrays of 0s and 1s, but I don't really see why. >Can you think of any reasons of why preserving logicalness takes precedent? ============== It's a universal rule in MATLAB that whenever you write a scalar of type A to an array element of type B, then a conversion from A to B takes place. Deviating from this rule for logicals only would create confusion and doesn't really make sense. Consider for example >> a=uint8(1:3) a =     1 2 3 >> a(1)=pi a =     3 2 3 You seem to think the result of this should have been a =     3.1416 2.0000 3.0000 but it seems purely subjective to me as to which casting precedence is the better one.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Walter Roberson Date: 6 May, 2010 19:31:37 Message: 22 of 40 Sergei Koulayev wrote: > 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. *My* expectation is that when I make an indexed assignment into a variable, that the variable will not change type underneath me according to the type of what was assigned in. What next A = 'Hello'; A(3) = 37; I wrote a number in there, so the character string should become numeric?? Or only if the number written in is non-integral or negative or exceeds 65537 and is thus not representable as a character? (Characters have been 16 bits internally since unicode was fully integrated.) A = uint8(250:255); A(2) = -5.5; Now -5.5 is not representable as uint8, so A should become.. what, int8? int16? float? double ?? The rule in Matlab is that a variable will change its type to the type of the assigned value only if the entire variable (unsubscripted) is named as the destination, such as A = uint8(250:255); A = -5.5; %A becomes double but not A = uint8(250:255); A(:) = -5.5; %A stays uint8 and gets assigned all 0 Everybody knows and expects that if you assign a floating point value to a seletion of an integral array that the floating point value will be clamped to the range representable by the integral type and that the value will be rounded. Why do you expect that a different mechanism be used for logical??
 Subject: What a hell is wrong with logical indexing??? Really??? From: Sergei Koulayev Date: 6 May, 2010 19:40:21 Message: 23 of 40 "Steven Lord" wrote in message ... .... > 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@mathworks.com > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > Steve: I know we are talking different languages here, you are the developer, I'm the user. As a user, I don't mind bumping up from logical to double, or from int8 to double, if that's necessary to give me a result I want. Just imagine that I create a variable X somewhere on the line #12 and it just happened to be stored at Integer. Or Logical. Then later at the line #133 I try to write X(3)=pi. I really want this "pi" to be there! Instead of doing just what I told him to do - just put the number in there - Matlab is *invisibly* correcting my operation, giving me a wrong answer that I didn't expect. Who would be happy about that? I want "what you see is what you get" as a consistent behavior.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Walter Roberson Date: 6 May, 2010 19:44:18 Message: 24 of 40 Sergei Koulayev wrote: > Do you agree that there > should be at least a warning when someone tries to write double into > logical? Not unless you are also going to start giving a warning every time someone uses a double (other than 0 or 1) in a logical context such as if VarName %same as if all(VarName ~= 0) or B = ~~A; %same as B = (A ~= 0) If we are going to have warnings, then let us be consistent and have the warnings whenever anyone _evaluates_ a double in a logical context and that double does not happen to be be 0.0 or 1.0. Wouldn't that be a squawk, the amount of code that would have to be touched up to avoid triggering "dbstop if warning"... if logical(VarName) %makes me think of if islogical(VarName)
 Subject: What a hell is wrong with logical indexing??? Really??? From: Matt J Date: 6 May, 2010 20:03:04 Message: 25 of 40 "Sergei Koulayev" wrote in message ... >Then later at the line #133 I try to write X(3)=pi. I really want this "pi" to be there! Instead of doing just what I told him to do - just put the number in there - Matlab is *invisibly* correcting my operation, giving me a wrong answer that I didn't expect. ==================== But then why wouldn't you define X (or convert it) to a data type that can hold pi. You say you don't want MATLAB to invisibly alter your choice of data, but you do seem to want it to invisibly alter your choice of data type. Why is one better than the other?
 Subject: What a hell is wrong with logical indexing??? Really??? From: Bruno Luong Date: 6 May, 2010 20:18:05 Message: 26 of 40 "Sergei Koulayev" wrote in message ... > Steve: I know we are talking different languages here, you are the developer, I'm the user. As a user, I don't mind bumping up from logical to double, or from int8 to double, if that's necessary to give me a result I want. Just imagine that I create a variable X somewhere on the line #12 and it just happened to be stored at Integer. Or Logical. Then later at the line #133 I try to write X(3)=pi. I really want this "pi" to be there! Instead of doing just what I told him to do - just put the number in there - Matlab is *invisibly* correcting my operation, giving me a wrong answer that I didn't expect. Who would be happy about that? I want "what you see is what you get" as a consistent behavior. Oh, but Matlab certainly gives "what you see is what you get", the difficulty for "user" is to see line #133 AND also line #12. If you see *only* line #133 then what you see is not what MATLAB see. After all, you are the one who code the line #12, isn't it? Do you have any valid excuse not seeing it? Bruno
 Subject: What a hell is wrong with logical indexing??? Really??? From: Walter Roberson Date: 6 May, 2010 20:22:17 Message: 27 of 40 Sergei Koulayev wrote: > As a user, I don't mind bumping up from logical > to double, or from int8 to double, if that's necessary to give me a > result I want. Just imagine that I create a variable X somewhere on the > line #12 and it just happened to be stored at Integer. Or Logical. Then > later at the line #133 I try to write X(3)=pi. I really want this "pi" > to be there! Instead of doing just what I told him to do - just put the > number in there - Matlab is *invisibly* correcting my operation, giving > me a wrong answer that I didn't expect. Who would be happy about that? I > want "what you see is what you get" as a consistent behavior. I know some languages that can do that with respect to some indexable objects (e.g., Maple lists), but I cannot think of _any_ language that will do that for objects that have been formally declared as arrays. I dunno... LISP might if anything does. What I would suggest to you, Sergei, is to use cell arrays for all of your work instead of numeric arrays. X{3} = pi; *will* change the datatype of X{3} to double. You will largely lose the ability to vectorize numeric computations, but if you want WYSIWYG storage then you are going to lose efficiency anyhow.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Sergei Koulayev Date: 6 May, 2010 20:27:05 Message: 28 of 40 "Matt J " wrote in message ... > "Sergei Koulayev" wrote in message ... > > >Then later at the line #133 I try to write X(3)=pi. I really want this "pi" to be there! > Instead of doing just what I told him to do - just put the number in there - Matlab is *invisibly* correcting my operation, giving me a wrong answer that I didn't expect. > ==================== > > But then why wouldn't you define X (or convert it) to a data type that can hold pi. You say you don't want MATLAB to invisibly alter your choice of data, but you do seem to want it to invisibly alter your choice of data type. Why is one better than the other? This is because my choice of data has first order effect on the correctness of the numerical results. While the choice of data type - these matter only for storage and maybe for efficiency of computations - clearly, secondary matters. Matlab agrees with this point and creates variables as doubles by default. Indeed, why would you store zeros(1,2) as a double? It is not efficient. Expecting users to track data types is not realistic. I guess this logical thing that I encountered is really the only place where problems might happen. But going from philosophy to practice, how
 Subject: What a hell is wrong with logical indexing??? Really??? From: us Date: 6 May, 2010 20:38:05 Message: 29 of 40 "Sergei Koulayev" wrote in message ... > People, > > check this out. I always thought I could treat 0-1 resulting from the logical operations as numbers. But apparently not. Why? It is completely unintuitive and dangerous. Can somebody give me a meaning of the following example? > > a = rand(100,1); > i = a<0.5; > i = i.^2; > i(i==0) = 2; > unique(i) > > ans = > > 1 > 2 > > i = a<0.5; > i(i==0) = 2; > unique(i) > > ans = > > 1 two things 1) there was a pre-LOGICAL era when any array ]0,1[ did the job 2) there is, indeed, an inconsistency on how ML treats LOGICALs     compared to other classes...      atf=false;      atf^pi % ans = 0      class(atf) % ans = logical      au8=uint8(1);      au8^pi %{ ??? Error using ==> power Integers can only be raised to positive integral powers. %} just a thought us
 Subject: What a hell is wrong with logical indexing??? Really??? From: Bruno Luong Date: 6 May, 2010 20:40:21 Message: 30 of 40 "Sergei Koulayev" wrote in message ... > Expecting users to track data types is not realistic. Of course it is. IMHO the three most important commands absolutely must known by new Matlab users are: DEBUG SIZE CLASS Bruno
 Subject: What a hell is wrong with logical indexing??? Really??? From: Matt J Date: 6 May, 2010 20:42:04 Message: 31 of 40 "Sergei Koulayev" wrote in message ... > This is because my choice of data has first order effect on the correctness of the numerical results. While the choice of data type - these matter only for storage and maybe for efficiency of computations - clearly, secondary matters. Matlab agrees with this point and creates variables as doubles by default. Expecting users to track data types is not realistic. =============== It is a mistake to assume this to be true of all or even most MATLAB users. Many users work very hard to make their code memory and speed efficient and painstakingly select/track the data types of their variables in order to do so. That's why MATLAB has the wide variety of data types that it does and even allows users to create new data types... Your quibbles are based on your own personal ideas of what is dispensible.
 Subject: What a hell is wrong with logical indexing??? Really??? From: Steven Lord Date: 6 May, 2010 20:52:38 Message: 32 of 40 "Bruno Luong" wrote in message news:hrv9fl\$1ue\$1@fred.mathworks.com... > "Sergei Koulayev" wrote in message > ... >> Expecting users to track data types is not realistic. > > Of course it is. > > IMHO the three most important commands absolutely must known by new Matlab > users are: > > DEBUG > SIZE > CLASS Perhaps this is optimistic, but I would hope that two others were also on that list; I would even put them before the three you mention: HELP DOC -- Steve Lord slord@mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
 Subject: What a hell is wrong with logical indexing??? Really??? From: us Date: 6 May, 2010 21:00:21 Message: 33 of 40 "Steven Lord" wrote in message ... > > "Bruno Luong" wrote in message > news:hrv9fl\$1ue\$1@fred.mathworks.com... > > "Sergei Koulayev" wrote in message > > ... > >> Expecting users to track data types is not realistic. > > > > Of course it is. > > > > IMHO the three most important commands absolutely must known by new Matlab > > users are: > > > > DEBUG > > SIZE > > CLASS > > Perhaps this is optimistic, but I would hope that two others were also on > that list; I would even put them before the three you mention: > > HELP > DOC > > -- > Steve Lord ...and let's not forget these      METHODS      METHODSVIEW us
 Subject: What a hell is wrong with logical indexing??? Really??? From: Walter Roberson Date: 6 May, 2010 21:19:00 Message: 34 of 40 Sergei Koulayev wrote: > Indeed, why would you store zeros(1,2) > as a double? It is not efficient. Not efficient in space, but efficient in CPU processing, as these days I understand it is not so uncommon to find chips whose double precision calculation throughput is faster than its best integer calculation performance. (The first commercial CPU that I am aware of that had this property was the MIPS R8000. I seem to recall hearing that some of the Intel Pentium line had the property that it was faster to add 32 bit integers by loading them into the floating point unit.)
 Subject: What a hell is wrong with logical indexing??? Really??? From: James Tursa Date: 6 May, 2010 21:29:05 Message: 35 of 40 "Sergei Koulayev" wrote in message ... > > Expecting users to track data types is not realistic. That is a ridiculous statement. If a programmer doesn't know about and keep track of the data types he/she is working with and the possible consequences of mixing them in operations in whatever language they are working with, then you have a very poor programmer that can't be trusted to write good code. James Tursa
 Subject: What a hell is wrong with logical indexing??? Really??? From: David Young Date: 6 May, 2010 21:39:04 Message: 36 of 40 Sorry to add to this rather long discussion - but two quick points: First, it's not a very strong argument to claim something is "unintuitive". Your intuition will be different to mine, especially if we have past experience of different languages. Anyway, if I used my intuition to tell me how to program, I don't think I'd have written a correct program in my life. Second, C uses the int type for logical data. Java - a more modern and arguably better designed language - has a separate boolean type, even though it follows C in many other ways. Why would the designers of the newer language introduce this distinction? Because it conveys a range of advantages, from efficiency to error checking.
 Subject: What a hell is wrong with logical indexing??? Really??? From: dpb Date: 6 May, 2010 21:42:21 Message: 37 of 40 Alan B wrote: ... > 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. Well, by not thinking the question "isn't outrageous", I'd say you're arguing for inconsistency... :) Undoubtedly being sorta' an ad hoc language that has evolved there are some inconsistencies in ML. _BUT_ to consider introducing such a major behaviorial change between arrays depending only on the values contained within is truly evil... :( --
 Subject: What a hell is wrong with logical indexing??? Really??? From: James Tursa Date: 6 May, 2010 22:35:06 Message: 38 of 40 "David Young" wrote in message ... > > Second, C uses the int type for logical data. Well, C does have the _Bool type. The fact that int is often used in programs for logical type data is a programmer choice, not a language specification. Any type that has zero and non-zero values could be used for this as they would have the same effect in logical contexts. > Java - a more modern and arguably better designed language ... Hmmmm ... inviting a flame war on the MATLAB newsgroup? James Tursa
 Subject: What a hell is wrong with logical indexing??? Really??? From: Steven Lord Date: 7 May, 2010 02:53:38 Message: 39 of 40 "Walter Roberson" wrote in message news:9nGEn.49\$7d5.4@newsfe17.iad... > Sergei Koulayev wrote: >> Indeed, why would you store zeros(1,2) >> as a double? It is not efficient. > > Not efficient in space, but efficient in CPU processing, as these days I > understand it is not so uncommon to find chips whose double precision > calculation throughput is faster than its best integer calculation > performance. Another reason (that I know Walter knows but Sergei may not) is preallocation. If you know that you want to end up with a 100-by-100 double precision matrix, but you don't know the values you want to be in the matrix when you first create it, it makes sense to allocate enough memory for 10000 elements once rather than allocating enough memory for 1 element, then 2 elements [copying the 1-element region into the 2-element region], then 3 elements [copying the 2-element region into the 3-element region], etc. Now we _could_ allocate that memory and just leave whatever bits are present in those memory locations, but if some amount of those elements are going to end up as zeros, you might want to fill them up with zeros initially and fill in just the nonzero elements. -- Steve Lord slord@mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
 Subject: What a hell is wrong with logical indexing??? Really??? From: Steve Eddins Date: 7 May, 2010 11:35:30 Message: 40 of 40 On 5/6/2010 5:00 PM, us wrote: > "Steven Lord" wrote in message > ... >> >> "Bruno Luong" wrote in message >> news:hrv9fl\$1ue\$1@fred.mathworks.com... >> > "Sergei Koulayev" wrote in message > >> ... >> >> Expecting users to track data types is not realistic. >> > >> > Of course it is. >> > >> > IMHO the three most important commands absolutely must known by new >> Matlab > users are: >> > >> > DEBUG >> > SIZE >> > CLASS >> >> Perhaps this is optimistic, but I would hope that two others were also >> on that list; I would even put them before the three you mention: >> >> HELP >> DOC >> >> -- >> Steve Lord > > ...and let's not forget these > > METHODS > METHODSVIEW > > us Don't forget MINDREAD.

### Everyone's Tags:

Separated by commas
Ex.: root locus, bode

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.