Re: Take out index
- From: "Oleg " <oleg.komarov@xxxxxxxxxx>
- Date: Wed, 24 Jun 2009 23:50:18 +0000 (UTC)
Nathan <ngreco32@xxxxxxxxx> wrote in message <c3448c04-efa4-463b-82c3-2e58518ba8d5@xxxxxxxxxxxxxxxxxxxxxxxxxxx>...
On Jun 24, 3:01?pm, "Diego Lass" <dlISC...@xxxxxxxxx> 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
.
- References:
- Take out index
- From: Diego Lass
- Re: Take out index
- From: Nathan
- Take out index
- Prev by Date: Re: Insert zeros into a vector
- Next by Date: fmincon, maximization, likelihood, constrain
- Previous by thread: Re: Take out index
- Next by thread: Operate on certain element
- Index(es):
Relevant Pages
|