From: Peter Bone on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hl3rrc$lbt$1(a)fred.mathworks.com>...
>
> "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
>

The result for x+=a should be the same as x=x+a. So in your examples [5 5 5] and [2 2 2].

Thanks Oleg. I realise it can be vectorised. I copied it directly from my c code. There are still situations outside of loops where it would be useful.
From: Jan Simon on
Dear Steven!

> ind = [1 2 3 2];
> x = [0 0 0];
> x(ind) += 5
>
> Should this return [5 10 5] or [5 5 5]?

I'd expect the same answer [5, 5, 5] as for:
x = [0, 0, 0];
[x(1), x(2), x(3), x(2)] = Inc(5, x(1), x(2), x(3), x(2))

% ---------------- 8< ----------------------
function varargout = Inc(A, varargin)
varargout = cell(1, nargin);
for iArg = 1:nargin
vargargout{iArg} = A + varargin{iArg};
end
% ---------------- >8 ---------------------

This is the usual behaviour for [a, a, a] = Func().
As a consequence, the Increment (or similar) operator *cannot* work inplace and would not be faster than the usual "x = x + 1".

Nevertheless, x++ would be okay (in my opinion):
x = [0,0,0]
x([1,2,3,2])++
==> x = [1,2,1]
But this would not be the common Matlab style or operators.

Kind regards, Jan
From: Walter Roberson on
Peter Bone wrote:
> Is there a way to use these kind of shortcuts like in C? It's annoying
> to have to write x=x+1.

No, there isn't.
From: Steve Amphlett on
"Peter Bone" <peterbone(a)hotmail.com> wrote in message <hl3tdr$377$1(a)fred.mathworks.com>...
>
> Thanks Oleg. I realise it can be vectorised. I copied it directly from my c code. There are still situations outside of loops where it would be useful.

Not many though. ++ notation is the stuff of loops. I agree that += would have a place in Matlab (might prevent the memory bloat of Big_array = Big_array + 1).

Why convert C code to ML though. Surely that's going the wrong way??
From: Steve Amphlett on
"Steve Amphlett" <Firstname.Lastname(a)Where-I-Work.com> wrote in message <hl5m4a$kjm$1(a)fred.mathworks.com>...
> "Peter Bone" <peterbone(a)hotmail.com> wrote in message <hl3tdr$377$1(a)fred.mathworks.com>...
> >
> > Thanks Oleg. I realise it can be vectorised. I copied it directly from my c code. There are still situations outside of loops where it would be useful.
>
> Not many though. ++ notation is the stuff of loops. I agree that += would have a place in Matlab (might prevent the memory bloat of Big_array = Big_array + 1).
>
> Why convert C code to ML though. Surely that's going the wrong way??

Wooo! I just realised that Octave has these operators. Interesting indeed.