From: Peter Bone on 12 Feb 2010 06:06 Is there a way to use these kind of shortcuts like in C? It's annoying to have to write x=x+1.
From: Oleg Komarov on 12 Feb 2010 06:17 "Peter Bone" > Is there a way to use these kind of shortcuts like in C? It's annoying to have to write x=x+1. What do you need it for, counting variables? x = 0; for ii = 1:100 x = x+1; end Oleg
From: Peter Bone on 12 Feb 2010 09:41 "Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <hl3dbh$cs7$1(a)fred.mathworks.com>... > "Peter Bone" > > Is there a way to use these kind of shortcuts like in C? It's annoying to have to write x=x+1. > What do you need it for, counting variables? > > x = 0; > for ii = 1:100 > x = x+1; > end > > > Oleg I need it for many things. Yes, something like you have there. For example, the first part of a least squares line fit I've written is n = length(points); sum_x = 0; sum_y = 0; sum_xx = 0; sum_xy = 0; vert = false; for i = 1 : n x = points(i,1); y = points(i,2); sum_x = sum_x + x; sum_y = sum_y + y; sum_xx = sum_xx + x*x; sum_xy = sum_xy + x*y; end The loop with the required operators would then become for i = 1 : n x = points(i,1); y = points(i,2); sum_x += x; sum_y += y; sum_xx += x*x; sum_xy += x*y; end Looks much nicer and maybe quicker since it only has to access the sum variable once.
From: Steven Lord on 12 Feb 2010 10:24 "Peter Bone" <peterbone(a)hotmail.com> wrote in message news:hl3pa1$4fq$1(a)fred.mathworks.com... > "Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message > <hl3dbh$cs7$1(a)fred.mathworks.com>... >> "Peter Bone" >> > Is there a way to use these kind of shortcuts like in C? It's annoying >> > to have to write x=x+1. >> What do you need it for, counting variables? x = 0; >> for ii = 1:100 >> x = x+1; >> end >> >> >> Oleg > > I need it for many things. Yes, something like you have there. For > example, the first part of a least squares line fit I've written is > > n = length(points); > sum_x = 0; > sum_y = 0; > sum_xx = 0; > sum_xy = 0; > vert = false; > > for i = 1 : n > x = points(i,1); > y = points(i,2); > sum_x = sum_x + x; > sum_y = sum_y + y; > sum_xx = sum_xx + x*x; > sum_xy = sum_xy + x*y; > end > > The loop with the required operators would then become > > for i = 1 : n > x = points(i,1); > y = points(i,2); > sum_x += x; > sum_y += y; > sum_xx += x*x; > sum_xy += x*y; > end > > Looks much nicer and maybe quicker since it only has to access the sum > variable once. As you can guess, this has been discussed in the past: http://www.mathworks.com/matlabcentral/newsreader/view_thread/246994#683966 As one example of a similar sort as the ones I posted in one of those earlier threads, what would you expect these examples to do? ind = [1 2 3 2]; x = [0 0 0]; x(ind) += 5 Should this return [5 10 5] or [5 5 5]? How about: ind = [1 2 3 2]; x = [1 1 1]; x(ind) += x(ind) Is the correct result [2 2 2] (x(2) gets incremented once using the value of x(2) prior to the statement), [2 3 2] (x(2) is incremented twice using the value of x(2) before the statement), or [2 4 2] (x(2) is incremented by 1 to 2, then incremented by the new current value of x(2))? -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Oleg Komarov on 12 Feb 2010 10:38
"Peter Bone" < > > "Peter Bone" > > > Is there a way to use these kind of shortcuts like in C? It's annoying to have to write x=x+1. > > What do you need it for, counting variables? > > > > x = 0; > > for ii = 1:100 > > x = x+1; > > end > > > > > > Oleg > > I need it for many things. Yes, something like you have there. For example, the first part of a least squares line fit I've written is > > n = length(points); > sum_x = 0; > sum_y = 0; > sum_xx = 0; > sum_xy = 0; > vert = false; > > for i = 1 : n > x = points(i,1); > y = points(i,2); > sum_x = sum_x + x; > sum_y = sum_y + y; > sum_xx = sum_xx + x*x; > sum_xy = sum_xy + x*y; > end > > The loop with the required operators would then become > > for i = 1 : n > x = points(i,1); > y = points(i,2); > sum_x += x; > sum_y += y; > sum_xx += x*x; > sum_xy += x*y; > end > > Looks much nicer and maybe quicker since it only has to access the sum variable once. I'll answer you in a different way. Matlab programming makes use of a different approach, it helps you to avoid loops: Example: points = [(1:10).' (11:20).']; sum_x = sum(points(:,1)); sum_y = sum(points(:,2)); sum_xx = sum(points(:,1).^2); sum_xy = sum(points(:,1).*points(:,2)); this is a vectorized solution for your problem. I know your qustion was different. Oleg |