How to increment a variable
Afficher commentaires plus anciens
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.
1 commentaire
amit dehury
le 28 Août 2019
variable=variable+1
Réponse acceptée
Plus de réponses (2)
Wayne King
le 24 Déc 2013
Modifié(e) : Walter Roberson
le 21 Jan 2022
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 commentaires
Nivethana Narayanan
le 28 Nov 2021
a = a+1 doesnt work as MATLAB complains that a is Unrecognized function or variable 'a'. is there a way to address this?
Luke Simonson
le 21 Jan 2022
You need to initialize a as a variable before you set a = a+1.
a = 0;
a = a+1;
Walter Roberson
le 24 Déc 2013
0 votes
You could create a class with preincrement and postincrement methods.
8 commentaires
KHADIJA
le 1 Jan 2014
Walter Roberson
le 1 Jan 2014
MATLAB classes are not simple. A beginner should just use a = a + 1
Jan
le 1 Jan 2014
And for advanced programmers, a=a+1 is a good choice also.
Francois Deneuville
le 17 Avr 2019
Modifié(e) : Francois Deneuville
le 17 Avr 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
le 17 Avr 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
le 17 Avr 2019
Modifié(e) : Francois Deneuville
le 17 Avr 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
le 17 Avr 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
le 29 Mai 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
Catégories
En savoir plus sur Performance and Memory dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!