Prev: DCNLP for solving optimal trajectory problems
Next: compiling matlab .m / generating .c (c code) from .m
From: Matthias Fripp on 13 Aug 2010 09:23 Does anyone know why this code doesn't work in Matlab? A=[1 -1 2 4] B=[0 0 0 0] I=(A<B) A(I)=B The first three assignments each give exactly what I'd expect (I=[0 1 0 0] and class(I)= 'logical'), but then I get this error message: "??? In an assignment A(I) = B, the number of elements in B and I must be the same." I checked size(I) and size(B), and they are indeed the same ([1 4]). I can fix it by writing A(I)=B(I), but it doesn't seem like I should have to. And that gets messy if I don't create the intermediate I: A(A<B)=B(A<B). Aside from this question, is there a "best" way to prevent elements of a vector from going below some value, while preserving NaNs? I can't use A=max(A,B) because that converts A to B if A is a NaN. Thanks for any help you can give! Matthias
From: Andy on 13 Aug 2010 09:37 "Matthias Fripp" <mfripp.remove.this(a)gmail.com> wrote in message <i43gvs$oj2$1(a)fred.mathworks.com>... > Does anyone know why this code doesn't work in Matlab? > > A=[1 -1 2 4] > B=[0 0 0 0] > I=(A<B) > A(I)=B > > The first three assignments each give exactly what I'd expect (I=[0 1 0 0] and class(I)= 'logical'), but then I get this error message: > > "??? In an assignment A(I) = B, the number of elements in B and I must be the same." > > I checked size(I) and size(B), and they are indeed the same ([1 4]). > > I can fix it by writing A(I)=B(I), but it doesn't seem like I should have to. And that gets messy if I don't create the intermediate I: A(A<B)=B(A<B). > > Aside from this question, is there a "best" way to prevent elements of a vector from going below some value, while preserving NaNs? I can't use A=max(A,B) because that converts A to B if A is a NaN. > > Thanks for any help you can give! > > Matthias 1) The error message is not so clear. But A(I) is of size 1x1, and you're trying to assign the 1x4 vector B to it. 2) What do you mean by "prevent elements of a vector from going below some value"? Do you want to remove these elements? Change them to the minimum value? Change them to NaN? A = rand(1,10); A(A<0.3) = NaN; % change to NaN all elements below 0.3
From: Wayne King on 13 Aug 2010 09:39
"Matthias Fripp" <mfripp.remove.this(a)gmail.com> wrote in message <i43gvs$oj2$1(a)fred.mathworks.com>... > Does anyone know why this code doesn't work in Matlab? > > A=[1 -1 2 4] > B=[0 0 0 0] > I=(A<B) > A(I)=B > > The first three assignments each give exactly what I'd expect (I=[0 1 0 0] and class(I)= 'logical'), but then I get this error message: > > "??? In an assignment A(I) = B, the number of elements in B and I must be the same." > > I checked size(I) and size(B), and they are indeed the same ([1 4]). > > I can fix it by writing A(I)=B(I), but it doesn't seem like I should have to. And that gets messy if I don't create the intermediate I: A(A<B)=B(A<B). > > Aside from this question, is there a "best" way to prevent elements of a vector from going below some value, while preserving NaNs? I can't use A=max(A,B) because that converts A to B if A is a NaN. > > Thanks for any help you can give! > > Matthias Hi Matthias, by entering A(I) you are returning A(2) because the logical vector, I, has only one nonzero entry at index two. Since B is 1x4, the error results. Are you trying to just set any element in A that is negative to zero? Then how about: A=[1 -1 2 4]; A(A<0)=0; This way will also preserve NaNs A=[1 -1 2 4 NaN]; A(A<0)=0; Wayne |