How to increment a variable

Hello. I couldn't find a way to increment a variable in MATLAB using the ++ operator. Can you help me, please? Is there a function or an operator that I can use?
Thanks in advance.

 Respuesta aceptada

John D'Errico
John D'Errico el 26 de Ag. de 2016
Editada: MathWorks Support Team el 22 de Mayo de 2019

7 votos

To increment a variable X, simply use
X = X+1;
MATLAB does not support the increment operator ++.

13 comentarios

Connor Ibach
Connor Ibach el 14 de Nov. de 2017
Yes
Xiwen Li
Xiwen Li el 29 de Dic. de 2017
Definitely yes.
Fernando Ortega Gallego
Fernando Ortega Gallego el 30 de Dic. de 2017
It doesn't matter how difficult writing that code is. The user had a reasonable doubt regarding Matlab syntax and you decide whether you wish to solve it or not, that's all.
John D'Errico
John D'Errico el 30 de Dic. de 2017
And I did show how to solve it, in a very simple way.
Jan
Jan el 30 de Dic. de 2017
Typing "a = a + 1" is difficult? What about this:
out = mat2cell(index(:)', 1, ...
diff([find([true;diff(index(:)) > 1]); numel(index)+1])');
Let's try this in C++. :-)
Kyle
Kyle el 21 de En. de 2018
What if you have a value which you want to increment, like this
long_variable_name(long_index1, long_index2, long_index3) += 1;
Is the recommended syntax
long_variable_name(long_index1, long_index2, long_index3) = long_variable_name(long_index1, long_index2, long_index3) + 1;
?
Walter Roberson
Walter Roberson el 21 de En. de 2018
"Is the recommended syntax"
If you are computing the indices, assign them to variables and use the variables in both places.
Kyle
Kyle el 21 de En. de 2018
Thanks Walter. Yes, in my example, the indices are integer variables with the names "long_index{1,2,3}". Is this what you're describing? The code is good as-is?
Walter Roberson
Walter Roberson el 22 de En. de 2018
Yes, that is fine. MATLAB will not need to recalculate anything in that case, only pull the indices out of memory, which is as efficient as MATLAB gets in most circumstances.
The only more efficient you could get would be for the case where you are certain that long_variable_name does not share contents with any other variable, in which case with some work it is possible to modify the variable "in-place"
Jhonler
Jhonler el 14 de Sept. de 2022
Can I use 'i++;'? like in c++.
Jan
Jan el 14 de Sept. de 2022
No, you can't do this in Matlab.
John D'Errico
John D'Errico el 14 de Sept. de 2022
@Jhonler you can try, but since it is not legal MATLAB syntax, it won't work. If all languages used exactly the same syntax and code, then we would only use one language.
Rev
Rev el 12 de Abr. de 2025
The one thing about being able to use syntax like C++ (the increment operation) is that when you want to try out ideas or concepts, Matlab makes it easier to test out sub sections of code instead of trying to compile and run it. It is helpful to be able to implement other language syntax in my experience. Matlab let's us try out code snippets and test out DSP operations with real time interface to hardware and equipment. Not to mention not having deal with the compiler.

Iniciar sesión para comentar.

Más respuestas (2)

Wayne King
Wayne King el 24 de Dic. de 2013
Editada: Walter Roberson el 21 de En. de 2022

3 votos

How about just
a = a+1;
a = 1;
x = zeros(10,1);
for k = 1:10;
x(a)= k^2;
a = a+1;
end
Of course, you would never write a loop for the above, or write the loop like that even if you did, but hopefully you get the point.

2 comentarios

Nivethana Narayanan
Nivethana Narayanan el 28 de Nov. de 2021
a = a+1 doesnt work as MATLAB complains that a is Unrecognized function or variable 'a'. is there a way to address this?
You need to initialize a as a variable before you set a = a+1.
a = 0;
a = a+1;

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 24 de Dic. de 2013

0 votos

You could create a class with preincrement and postincrement methods.

8 comentarios

KHADIJA
KHADIJA el 1 de En. de 2014
thanks for answering,but am just a biginer and i don t know much about matlab, could you please explian this method to me in a simple way
Walter Roberson
Walter Roberson el 1 de En. de 2014
MATLAB classes are not simple. A beginner should just use a = a + 1
Jan
Jan el 1 de En. de 2014
And for advanced programmers, a=a+1 is a good choice also.
Francois Deneuville
Francois Deneuville el 17 de Abr. de 2019
Editada: Francois Deneuville el 17 de Abr. de 2019
I just made a Class to make the i++ (or the ++i ? - i am not a C expert) functionnality happenning in Matlab.
You can try this one out.
François
classdef Ipp < handle
% Simulate in Matlab the i++ functionality
%
% Example 1:
% i = Ipp
% i.p % => 1
% i.p % => 2
% i.p % => 3
% i.i % => 3
% i.i % => 3
%
% Example 2:
% i = Ipp(5)
% i.p % => 6
% i.p % => 7
% cos(pi/8 * i.p) % => -1
% i.i % => 8
properties (SetAccess = private, GetAccess = public)
i = [];
end
methods
function self = Ipp(varargin) % Constructor
p = inputParser;
addOptional(p, 'ini', 0, @isnumeric);
parse(p, varargin{:});
self.i = p.Results.ini;
end
function out = p(self)
self.i =self.i + 1;
out = self.i;
end
end
end
Walter Roberson
Walter Roberson el 17 de Abr. de 2019
However, you cannot do any further arithmetic on the object, such as comparing the value to a bound: you can only get the value of the variable immediately after incrementing it.
Francois Deneuville
Francois Deneuville el 17 de Abr. de 2019
Editada: Francois Deneuville el 17 de Abr. de 2019
I think you can if you type i.i
then you get the current value
I just mofified the comments to make this more clear
Francois Deneuville
Francois Deneuville el 17 de Abr. de 2019
By the way, Walter, thanks for you comment:
You could create a class with preincrement and postincrement methods.
it was a great idea
Jesús Ramos
Jesús Ramos el 29 de Mayo de 2021
What you implemented is the ++i operator of C (or java), where the variable is increased before returning the value, if you want to implement the i++ operator, you would have to add another method to the class:
function out = post_add(self)
temp=self.i;
self.i=self.i+1;
out=temp;
end

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Dic. de 2013

Comentada:

Rev
el 12 de Abr. de 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by