So I have a class let's call it MyClass
which has 3 properties, a value
, valueMinus1
and valuePlus1
. These two properties hold the neighboring values in an array of classes. Is there a good way to update these (it will be done iteratively as value
changes). There is the simple way:
%//initialize the class array
class_array = MyClass.empty(n,0);
for i=1:n
class_array(i) = MyClass(i); % //just put in the index at value
end
%//now update the valuePlus1 and valueMinus1 properties
for i=1:n
if i==1
class_array(i).valueMinus1 = class_array(end).value;
else
class_array(i).valueMinus1= class_array(i-1).value
end
if i==n
class_array(i).valuePlus1 = class_array(1).value
else
class_array(i).valuePlus1 = class_array(i+1).value
end
But I feel like there must be a better, more clever and more efficient way to do this.