|
Nathan <ngreco32@gmail.com> wrote in message <c3448c04-efa4-463b-82c3-2e58518ba8d5@o5g2000prh.googlegroups.com>...
> On Jun 24, 3:01?pm, "Diego Lass" <dlISC...@gmail.com> wrote:
> > Hi
> > For a problem,
> > A = [ 1; 2; 3; 4; 5; 6; 7 ]
> > A =
> > ? 1
> > ? 2
> > ? 3
> > ? 4
> > ? 5
> > ? 6
> > ? 7
> >
> > I know that I want to add 2 to each element, expect for the entries 1:2:end
> > i.e. I want to get a function f()
> > B = f(A) such that
> > B =
> > ? 1
> > ? 3
> > ? 3
> > ? 6
> > ? 5
> > ? 8
> > ? 7
> > what is the most efficient way to do this? ?Remember add 2 is just a simple example, and the vector A might be really huge
>
> What about
> A(2:2:end) = A(2:2:end)+2;
> B = A;
>
> Simple enough?
Considering as suggested by Nathan your function would be:
function Out = ArrayInc(In_array, Increment, OddEven)
% Check part (up to you)
% Engine part
if nargin == 2
Even = 1;
else
whichcase = [0,1];
IDX = strcmpi(OddEven, {'odd', 'even'});
Even = whichcase(IDX);
end
In_array(1+Even:2:end) = In_array(1+Even:2:end)+Increment;
Out = In_array;
end
The OddEven input must be string. If you write 'even', even - positioned elements in the input array wil be incremented by "Increment", else odd - positioned el.
Cheers
|