COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### set indexing( i+1) as a variable

• Email
• Follow

```I am having a problem. Say every time cl2 is greater than cl1 I want to know what
cl1(i+1) is. This worked just like I thought it would. I did it below.

cl1=[5;10;15;20;25;30]
cl2=[6;11;14;21;24;31]
for i = 1:length(cl1)-1
if cl2(i)>cl1(i)
B=cl1(i+1)
end
end

the output was B=10, B=15,B=25
My problem is that I would like to make the i+1 part of my code a variable, so I could change that part to be ( i+1) or (i+2) or just (i) or just about anything that works.  So I tried to make the (i+1) part a variable but it does not seem to work.  Below I made ii a variable and put it in the code where B=cl1(ii)

ii=i+1
cl1=[5;10;15;20;25;30]
cl2=[6;11;14;21;24;31]
for i = 1:length(cl1)-1
if cl2(i)>cl1(i)
B=cl1(ii)
end
end
??? Subscript indices must either be real positive integers or logicals.
Error in ==> manipVect at 41
B=cl1(ii)
This error message makes it seem like my indexing is wrong, but I know it worked before I tried to make ( i+1 )   into a variable so I think the way I am making i+1 into a variable is wrong. Could you tell me the right way to turn an index into a variable?
Thank you,
```
 0

See related articles to this posting

```On 10-08-30 06:20 PM, jonathan wrote:
> I am having a problem. Say every time cl2 is greater than cl1 I want to
> know what cl1(i+1) is. This worked just like I thought it would. I did
> it below.
>
> cl1=[5;10;15;20;25;30]
> cl2=[6;11;14;21;24;31]
> for i = 1:length(cl1)-1
> if cl2(i)>cl1(i)
> B=cl1(i+1)
> end
> end
>
> the output was B=10, B=15,B=25
> My problem is that I would like to make the i+1 part of my code a
> variable, so I could change that part to be ( i+1) or (i+2) or just (i)
> or just about anything that works.

ii=1;
cl1=[5;10;15;20;25;30]
cl2=[6;11;14;21;24;31]
for i = 1:length(cl1)-ii
if cl2(i)>cl1(i)
B=cl1(i+ii)
end
end

Alternately the whole thing can be vectorized:

offset=1;
B = cl1(find(cl2(1:end-offset)>cl1(1:end-offset))+offset)
```
 0

```Walter Roberson <roberson@hushmail.com> wrote in message <i5her4\$lgm\$1@canopus.cc.umanitoba.ca>...
> On 10-08-30 06:20 PM, jonathan wrote:
> > I am having a problem. Say every time cl2 is greater than cl1 I want to
> > know what cl1(i+1) is. This worked just like I thought it would. I did
> > it below.
> >
> > cl1=[5;10;15;20;25;30]
> > cl2=[6;11;14;21;24;31]
> > for i = 1:length(cl1)-1
> > if cl2(i)>cl1(i)
> > B=cl1(i+1)
> > end
> > end
> >
> > the output was B=10, B=15,B=25
> > My problem is that I would like to make the i+1 part of my code a
> > variable, so I could change that part to be ( i+1) or (i+2) or just (i)
> > or just about anything that works.
>
> ii=1;
> cl1=[5;10;15;20;25;30]
> cl2=[6;11;14;21;24;31]
> for i = 1:length(cl1)-ii
>      if cl2(i)>cl1(i)
>          B=cl1(i+ii)
>      end
> end
>
>
> Alternately the whole thing can be vectorized:
>
> offset=1;
> B = cl1(find(cl2(1:end-offset)>cl1(1:end-offset))+offset)

Mr. Roberson, that is incredible how quickly you did that, and yes it works,Thanks.
I would really like to know  if i could make the index into a varialbe, like i tried to do in my example above?  It would help to know because the real problem i have is on a much larger for loop then the example i made to show you.

can i make i+1 into a varialbe , like  ii=(i+1)  and then attach that variable to a vector named  A like:   A(ii)   , where we know ii=(i+1)?

thanks
```
 0

```On 10-08-30 06:48 PM, jonathan wrote:

> I would really like to know if i could make the index into a varialbe,
> like i tried to do in my example above? It would help to know because
> the real problem i have is on a much larger for loop then the example i
>
> can i make i+1 into a varialbe , like ii=(i+1) and then attach that
> variable to a vector named A like: A(ii) , where we know ii=(i+1)?

Where you write "ii=(i+1)" you are not really trying to define a variable:
instead you are trying to define a formula, a rule that transforms "i" in to a
(usually) different value.

You have two major possibilities: you can define a function to do the
transformation, or you can work symbolically.

Routine example of using a function:

ii = @(x) x + 1;

This defines "ii" as being a function handle for a function that takes a
single parameter whose name of convenience is "x" and returning one more than
the value of that parameter.

Then later, you would use it as,

A(ii(i))

This use standard function call syntax to invoke the function ii with a
parameter which is the current value of i.

Advanced example of using a function:

Save the following in incrtest.m -- do not attempt this from the command line.

function incrtest
offset = 1        %MUST be initialized before the below "function"
i = 5              %MUST be assigned a value before the following line
function P = ii    %define a nested function. MUST be in the same function
P = i + offset;
end                %the "end" is mandatory here
A(ii) = 9
i = 2
A(ii) = 4
offset = 2
A(ii) = 7
end                  %this "end" statement is mandatory!!

Now invoke incrtest and study the outputs.

This advanced example uses nested functions and closures, considerably more
advanced techniques -- but it allows you to write the remaining code in the
style you were hoping for.

Symbolically, you would use

offset = 1
sym i;            %this gives a value to the variable named "i"
ii = i + offset;  %this gives a symbolic expression to the variable named "ii"

Then later you would use, e.g.

A = 1:5
for i = 1 : 3    %this for loop assigns a new value to "i"
A(subs(ii))    %this evaluates the symbolic expression stored in ii,
%replacing the symbolic variable i that is inside it
%with the current numeric value of i
end

Although this method is the closest in spirit to the way you originally wrote
the code, you can see that it is in some ways the clumsiest of the methods. Oh
yes, and it also depends upon you having a license for the symbolic toolkit,
which is included in the student edition but is an extra-cost for all the
other license forms. Also, statements that use the symbolic toolkit cannot be
compiled by the Matlab Compiler toolkit.

The mechanism that allows you to write the code in the loop most closely to
how you wanted is the most advanced technique and hence the most likely to
make a mistake with. The mechanism that allows you to write the rule most
closely to how you wanted is clumsy to use in practice (and easy to make
mistakes with.)

```
 0

```Thanks Mr. Roberson, I am working on the function approach.
```
 0