From: Judy on 27 Jul 2010 20:19 Hi everybody, I am so confused by MATLAB's rounding. I am trying to build a matrix with units that I am familiar with (Hz).. That's the reason why I didn't stick with integer indexing in the beginning (i.e. i=1,2,3..). I simplified my problem down below in 2 cases and both looks to round so that I am not able to get an integer for indexing my matrix. I could just round the ii variable. Or I could just not do the conversion in the beginning. Is there any other elegant solution to this? Much thanks!! %Try 1: int=.2; for i=1:int:10, ii=(i-1)/int+1 for m=1:5, %a(m,ii)=randn; end end %Try 2: int=.2; i=1; while i <= 10, ii=(i-1)/int+1 for m=1:5, %a(m,ii)=randn; end i=i+int; end
From: Judy on 27 Jul 2010 22:03 Still puzzled... When doing the iteration, i=1 is OK. ii-round(ii)=0, but when i=2, ii-round(ii)=-eps Why is this happening? :(
From: TideMan on 27 Jul 2010 22:25 On Jul 28, 12:19 pm, "Judy " <sauwen...(a)gmail.com> wrote: > Hi everybody, > > I am so confused by MATLAB's rounding. I am trying to build a matrix with units that I am familiar with (Hz).. That's the reason why I didn't stick with integer indexing in the beginning (i.e. i=1,2,3..). I simplified my problem down below in 2 cases and both looks to round so that I am not able to get an integer for indexing my matrix. > > I could just round the ii variable. Or I could just not do the conversion in the beginning. Is there any other elegant solution to this? > > Much thanks!! > > %Try 1: > int=.2; > for i=1:int:10, > ii=(i-1)/int+1 > for m=1:5, > %a(m,ii)=randn; > end > end > > %Try 2: > int=.2; > i=1; > while i <= 10, > ii=(i-1)/int+1 > for m=1:5, > %a(m,ii)=randn; > end > i=i+int; > end Try 3: a=randn(5,10);
From: TideMan on 27 Jul 2010 22:29 On Jul 28, 2:03 pm, "Judy " <sauwen...(a)gmail.com> wrote: > Still puzzled... > > When doing the iteration, i=1 is OK. > > ii-round(ii)=0, > > but when i=2, ii-round(ii)=-eps > > Why is this happening? :( Floating point operations are seldom exact. See here: http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F
From: Mark Shore on 28 Jul 2010 02:45 "Judy " <sauwen.jl(a)gmail.com> wrote in message <i2nt28$h93$1(a)fred.mathworks.com>... > Hi everybody, > > I am so confused by MATLAB's rounding. I am trying to build a matrix with units that I am familiar with (Hz).. That's the reason why I didn't stick with integer indexing in the beginning (i.e. i=1,2,3..). I simplified my problem down below in 2 cases and both looks to round so that I am not able to get an integer for indexing my matrix. > > I could just round the ii variable. Or I could just not do the conversion in the beginning. Is there any other elegant solution to this? > > Much thanks!! > > %Try 1: > int=.2; > for i=1:int:10, <snip> What on Earth is the problem with integral indexing? The rest of the world seems to find it natural, with the only controversy being whether to start counting at zero or one. Without getting into vectorization or other techniques to make your code more elegant (and sometimes faster), the following is equivalent and better behaved: int = 1; for i = 5:int:50
|
Pages: 1 Prev: How to move a point on a map Next: Convert 3D slice in matrix |