Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Attempted to access cL(1.5); index must be a positive integer or logical.

Subject: Attempted to access cL(1.5); index must be a positive integer or logical.

From: Edman

Date: 8 Nov, 2010 15:32:05

Message: 1 of 4

I'm trying to run through an array of x values ranging from [0: .5: 90] with a for loop. Within the for loop, I have equations that return a value for both cD, and cL.

Two issues that I'm having:

1. Attempted to access cL(1.5); index must be a positive integer or logical.

2. Results for x, cD, and cL continue to overwrite previous values when my equations was formatted as:
cL = 7E-15*x^5 - 6E-14*x^4 - 5E-13*x^3 + 5E-12*x^2 + 0.11*x - 4E-11;

To try and overcome this, I have since reformatted my equation as:
cL(x) = 7E-15*x^5 - 6E-14*x^4 - 5E-13*x^3 + 5E-12*x^2 + 0.11*x - 4E-11;

Since I did the most recent format of the equation, I continue to get errors about:
Attempted to access cL(1.5); index must be a positive integer or logical.

How do I return my results for cD, and cL to a matrix, and not overwrite the previous data? How do I have my "For" loop run through all my x values? I have even tried starting at x = [1: .5: 90]. Then MatLab rejects it for the 1.5 value.

Here is the first part of my code:
for x = (1:.5:90);
    
    if x >= 1 && x <= 5
        cL(x) = 7E-15*x^5 - 6E-14*x^4 - 5E-13*x^3 + 5E-12*x^2 + 0.11*x - 4E-11;
        cD(x) = 2E-06*x^5 - 2E-05*x^4 + 6E-05*x^3 + 7E-05*x^2 - 1E-05*x + 0.0133;

Subject: Attempted to access cL(1.5); index must be a positive integer

From: Walter Roberson

Date: 8 Nov, 2010 15:42:46

Message: 2 of 4

On 08/11/10 9:32 AM, Edman wrote:

> To try and overcome this, I have since reformatted my equation as:
> cL(x) = 7E-15*x^5 - 6E-14*x^4 - 5E-13*x^3 + 5E-12*x^2 + 0.11*x - 4E-11;

You are attempting in that statement to define a function named cL, but
that is not the syntax for defining functions.

If you have a reasonably recent Matlab, you could try

cL = @(x) 7E-15*x^5 - 6E-14*x^4 - 5E-13*x^3 + 5E-12*x^2 + 0.11*x - 4E-11;

Subject: Attempted to access cL(1.5); index must be a positive integer or logical.

From: Sean

Date: 8 Nov, 2010 15:44:05

Message: 3 of 4

"Edman " <maver1ck01@aol.com> wrote in message <ib955l$hkl$1@fred.mathworks.com>...
> I'm trying to run through an array of x values ranging from [0: .5: 90] with a for loop. Within the for loop, I have equations that return a value for both cD, and cL.
>
> Two issues that I'm having:
>
> 1. Attempted to access cL(1.5); index must be a positive integer or logical.
>

The index is the spot it's in and is only a positive integer. I.e. first spot, second spot third etc.

cL(cL==1.5)

Will give you a logical vector that is true to the index you want.

If you want the actual index
find(cL==1.5)
However, this can usually be avoided with logical indexing.

Subject: Attempted to access cL(1.5); index must be a positive integer

From: Walter Roberson

Date: 8 Nov, 2010 16:13:12

Message: 4 of 4

On 08/11/10 9:44 AM, Sean wrote:
> "Edman " <maver1ck01@aol.com> wrote in message
> <ib955l$hkl$1@fred.mathworks.com>...
>> I'm trying to run through an array of x values ranging from [0: .5:
>> 90] with a for loop. Within the for loop, I have equations that return

> cL(cL==1.5)

> Will give you a logical vector that is true to the index you want.

It happens that N+0.5 for N an integer in 0 to 89, are values that are
exactly representable in binary floating point, so the suggested code
can actually work. It is, however, not recommended that one rely upon
that property, as relatively few short decimal fractions are exactly
representable in binary floating point, and coding in terms of one of
the few that happen to work tends to lead to programmer laziness about
dealing with the difficulties in floating point representation. For
example, if the array of x values were generated as [0 : .1 : 90]
instead, it is not promised that the _exact_ value 1.5 would be in the
array.

Tags for this Thread

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.

Contact us