Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Increment shortcuts (++, +=)

Subject: Increment shortcuts (++, +=)

From: Peter Bone

Date: 12 Feb, 2010 11:06:02

Message: 1 of 13

Is there a way to use these kind of shortcuts like in C? It's annoying to have to write x=x+1.

Subject: Increment shortcuts (++, +=)

From: Oleg Komarov

Date: 12 Feb, 2010 11:17:05

Message: 2 of 13

"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

Subject: Increment shortcuts (++, +=)

From: Peter Bone

Date: 12 Feb, 2010 14:41:05

Message: 3 of 13

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <hl3dbh$cs7$1@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.

Subject: Increment shortcuts (++, +=)

From: Steven Lord

Date: 12 Feb, 2010 15:24:30

Message: 4 of 13


"Peter Bone" <peterbone@hotmail.com> wrote in message
news:hl3pa1$4fq$1@fred.mathworks.com...
> "Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message
> <hl3dbh$cs7$1@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@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Increment shortcuts (++, +=)

From: Oleg Komarov

Date: 12 Feb, 2010 15:38:06

Message: 5 of 13

"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

Subject: Increment shortcuts (++, +=)

From: Peter Bone

Date: 12 Feb, 2010 15:51:23

Message: 6 of 13

"Steven Lord" <slord@mathworks.com> wrote in message <hl3rrc$lbt$1@fred.mathworks.com>...
>
> "Peter Bone" <peterbone@hotmail.com> wrote in message
> news:hl3pa1$4fq$1@fred.mathworks.com...
> > "Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message
> > <hl3dbh$cs7$1@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@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.

Subject: Increment shortcuts (++, +=)

From: Jan Simon

Date: 12 Feb, 2010 16:00:26

Message: 7 of 13

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

Subject: Increment shortcuts (++, +=)

From: Walter Roberson

Date: 12 Feb, 2010 16:42:13

Message: 8 of 13

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.

Subject: Increment shortcuts (++, +=)

From: Steve Amphlett

Date: 13 Feb, 2010 07:59:06

Message: 9 of 13

"Peter Bone" <peterbone@hotmail.com> wrote in message <hl3tdr$377$1@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??

Subject: Increment shortcuts (++, +=)

From: Steve Amphlett

Date: 17 Feb, 2010 13:49:04

Message: 10 of 13

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <hl5m4a$kjm$1@fred.mathworks.com>...
> "Peter Bone" <peterbone@hotmail.com> wrote in message <hl3tdr$377$1@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.

Subject: Increment shortcuts (++, +=)

From: Matt J

Date: 17 Feb, 2010 15:03:06

Message: 11 of 13


Here's another thread where this was discussed,

http://www.mathworks.com/matlabcentral/newsreader/view_thread/252986#654962

and below is a slightly upgraded version of the @FancyNumeric class that I proposed there. It lets you do things like the following



>> x=FancyNumeric([3 4])

x =

     3 4
 
>> x.p=1; %same as x+=1

x =

     4 5
 
>> x.m=2 %same as x-=2

x =

     2 3
 
>> x.t=3 %same as x*=3

x =

     6 9




%%%%%%%%%%%%%%%%%%%
classdef FancyNumeric<double

    methods
        
        function obj=FancyNumeric(data)
            obj=obj@double(data);
        end
        
        function obj=subsref(obj,IndexStruct)
  
           obj=FancyNumeric(builtin('subsref',obj.data,IndexStruct));
 
        end
        
        
        function obj=subsasgn(obj,IndexStruct,RHS)
           
              x=double(obj);
    
              

              
             switch IndexStruct.type
                 
                 case '.'
              
                   switch IndexStruct.subs

                        case 'p'
                            
                          x=x+RHS;
                            
                        case 'm'
                            
                          x=x-RHS;
                            
                        case 't'
                            
                           x=x.*RHS;
                            
                        case 'd'
                            
                            x=x./RHS;
                            
                        otherwise
                            
                            error 'Unsupported operation'
                            
                   end%subs
                   
                 otherwise%numeric assignment
                     
                    x=builtin('subsasgn',x,IndexStruct,RHS);
                   
                    
               
             end%type
           
           obj=FancyNumeric(x);
        end
        

        function display(obj)
                      
            l=inputname(1);
            T=evalc('double(obj)'); %only way to get at the builtin display method
            if ~isempty(l),
                 T=strrep(T,'ans =',[l ' =']);
            end

            jj=find(T~=sprintf('\n'),1,'last');
            
            T=T(1:jj);
           
            disp(T), disp ' '
            
        end
        
        
    end
    
end

Subject: Increment shortcuts (++, +=)

From: Diogo

Date: 23 Aug, 2013 12:42:08

Message: 12 of 13

"Peter Bone" <peterbone@hotmail.com> wrote in message <hl3cmq$1fo$1@fred.mathworks.com>...
> Is there a way to use these kind of shortcuts like in C? It's annoying to have to write x=x+1.

Hi!

If you are looking for a more elegant solution (and faster, according to tic toc) you can use:

x=plus(x,1);

Diogo

Subject: Increment shortcuts (++, +=)

From: Judson

Date: 25 Nov, 2013 02:04:06

Message: 13 of 13

> 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]?
>

Same as
x(ind) = x(ind) + 5;


> How about:
>
> ind = [1 2 3 2];
> x = [1 1 1];
> x(ind) += x(ind)

Same as x(ind) = x(ind) + x(ind);

I don't understand why that is not obvious.

I also don't understand why you wouldn't want to make this code a lot cleaner and less likely to introduce bugs:

some_huge_array(blah_37(3), :, junk(junk(2))) = some_huge_array(blah_37(3), :, junk(junk(2))) + 1;

God forbid you need to change one of those parameters, and change it identically in both places. Or, for that matter, reading it is exponentially more complicated without an increment operator.

Tags for this Thread

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.

Contact us