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

### error:Attempted to access F2(6); index must be a positive integer or logical.

• Email
• Follow

```I am trying to create an array inside a for loop and then I will plot it in MATLAB. but I am tring to do it by incrementing the variable by 0.01 at one time. like this:

for w = 0.01:0.01:20
F2(100*w) = G/abs(w^4 - C*G*w^2 + G);
end

it gives me the error : Attempted to access F2(6); index must be a positive integer or logical.

this seems silly because 6 is a positive integer index and also it can make the code work until 6 but at 6 it gives an error

what am I doing wrong?? is there any other way for me to do what I want??

```
 0

See related articles to this posting

```"Kerem  tezcan" <kctezcan@gmail.com> wrote in message <ibcdd8\$t2s\$1@fred.mathworks.com>...
> I am trying to create an array inside a for loop and then I will plot it in MATLAB. but I am tring to do it by incrementing the variable by 0.01 at one time. like this:
>
> for w = 0.01:0.01:20
>       F2(100*w) = G/abs(w^4 - C*G*w^2 + G);
> end
>
> it gives me the error : Attempted to access F2(6); index must be a positive integer or logical.
>
> this seems silly because 6 is a positive integer index and also it can make the code work until 6 but at 6 it gives an error
>
> what am I doing wrong?? is there any other way for me to do what I want??
>

dont do this in a for loop, the following is faster:

w = 0.01:0.01:20;
F2 = G/abs(w.^4 - C*G*w.^2 + G);
```
 0

```On 10-11-09 03:11 PM, Kerem tezcan wrote:

> for w = 0.01:0.01:20
> F2(100*w) = G/abs(w^4 - C*G*w^2 + G);
> end

It is not possible for binary floating point to exactly represent 0.01, and
not potentially possible to exactly represent any binary fractions in your
specified range other than 0.25, 0.5, 0.75, 1.0, and integers added to those
values. When those inexact binary representations are multiplied by 100, the
resulting number will often not be an exact integer, but the difference
between an exact integer and the actual number might be so small that the
error message rounds off to the integer during the printout.

The difference between 6 and the 6-like entry that appears in
(0.01:.01:20)*100 is only 8.88178419700125e-16 but that is enough for the
indexing routine to complain about it being non-integral.

You are advised to use full integers:

for w = 1:2000
F2(w) = G/abs((w/100)^4 - C*G*(w/100)^2 + G);
end

Which of course can be recoded as

w = 1:2000;
F2 = G/abs((w/100).^4 - C*G*(w/100).^2 + G);

without any loop.
```
 0

```
"Kerem  tezcan" <kctezcan@gmail.com> wrote in message
news:ibcdd8\$t2s\$1@fred.mathworks.com...
> I am trying to create an array inside a for loop and then I will plot it
> in MATLAB. but I am tring to do it by incrementing the variable by 0.01 at
> one time. like this:
>
> for w = 0.01:0.01:20
>      F2(100*w) = G/abs(w^4 - C*G*w^2 + G);
> end
>
> it gives me the error : Attempted to access F2(6); index must be a
> positive integer or logical.
>
> this seems silly because 6 is a positive integer index and also it can
> make the code work until 6 but at 6 it gives an error

In this scenario, 100*w is NOT exactly 6 but is very close to 6 (so close

> what am I doing wrong?? is there any other way for me to do what I want??

w = 0.01:0.01:20;
F2 = zeros(size(w)); % Make sure to preallocate
for k = 1:numel(w)
F2(k) = G/abs(w(k)^4-C*G*w(k)^2+G);
end

or:
w = 0.01:0.01:20;
F2 = G./abs(w.^4-C*G*w.^2+G);

--
Steve Lord
slord@mathworks.com
http://www.mathworks.com

```
 0

3 Replies
868 Views

Similar Articles

11/30/2013 3:57:03 PM
page loaded in 4826 ms. (0)