Thread Subject: Take out index

Subject: Take out index

From: Diego Lass

Date: 24 Jun, 2009 22:01:02

Message: 1 of 3

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

Subject: Take out index

From: Nathan

Date: 24 Jun, 2009 22:17:46

Message: 2 of 3

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?

Subject: Take out index

From: Oleg

Date: 24 Jun, 2009 23:50:18

Message: 3 of 3

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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix operation Diego Lass 24 Jun, 2009 18:04:01
rssFeed for this Thread

Contact us at files@mathworks.com