for loop on decimal numbers fails.

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

for loop on decimal numbers fails.

Swiss_Knight
This post was updated on .
Hi,

if we loop on values which are not integers, it fails.

Here is an example :
A = [
         0.1         0.5      2.0983
         0.2           1      3.6153
         0.3         1.5      7.4621
         0.4           2      5.6493
];
I=0.1:0.1:0.4;
J=0.5:0.5:2;

for i=0.1:0.1:0.4
  for j=0.5:0.5:2;
    M(i,(find(J==j)))=[A((find((A(:,1)==i & A(:,2)==j))),3)];
  endfor;
endfor;


=> returns :
ans =          1
error: subscript indices must be either positive integers less than 2^31 or logicals.



Outside the loops every part looks fine :
(find(J==1.5)) => 3  [it's OK]
A((find((A(:,1)==i & A(:,2)==j))),3) => ans =     2.0983  [it's OK]

But :
A((find((A(:,1)==0.2 & A(:,2)==1.5))),3) => ans = [](0x1)  [it's no more OK... why?!]

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: for loop on decimal numbers fails.

James Sherman
On Tue, Jul 19, 2016 at 12:59 PM, Swiss_Knight <[hidden email]> wrote:

> Hi,
>
> if we loop on values which are not integers, it fails.
>
> Here is an example :
> A = [
>          0.1         0.5      2.0983
>          0.2           1      3.6153
>          0.3         1.5      7.4621
>          0.4           2      5.6493
> ];
> I=0.1:0.1:0.4;
> J=0.5:0.5:2;
>
> for i=0.1:0.1,0.4
>   for j=0.5:0.5:2;
>     M(i,(find(J==j)))=[A((find((A(:,1)==i & A(:,2)==j))),3)];
>   endfor;
> endfor;
>
>
> => returns :
> /ans =          1
> error: subscript indices must be either positive integers less than 2^31 or
> logicals./
>
>
> Outside the loops every part looks fine :
> /(find(J==1.5))/ => 3  [it's OK]
> /A((find((A(:,1)==i & A(:,2)==j))),3)/ => /ans =     2.0983/  [it's OK]
>
> But :
> /A((find((A(:,1)==0.2 & A(:,2)==1.5))),3)/ => /ans = [](0x1)/  [it's no more
> OK... why?!]
>
> Thanks.
>
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/for-loop-on-decimal-numbers-fails-tp4678664.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave


Hi Swiss,

I believe you have a typo here:

> for i=0.1:0.1,0.4
>   for j=0.5:0.5:2;
>     M(i,(find(J==j)))=[A((find((A(:,1)==i & A(:,2)==j))),3)];
>   endfor;
> endfor;

The first line (I believe) should be

> for i=0.1:0.1:0.4

Note the colon and not a comma.  Further, in the line:

>     M(i,(find(J==j)))=[A((find((A(:,1)==i & A(:,2)==j))),3)];

Your variable "i" can take non-integer values (in particular the first
value it takes in the loop is 0.1), and in Octave you can't use
non-integer values as an index into an array.  (Hence the error that
Octave produces).  One way you could fix this (though certainly not
the only way) could be something like:

> i_values = 0.1:0.1:0.4;
> for i_index = 1:length(i_values),
>   i = i_values(i_index)
>   for j = 0.5:0.5:2;
>     M(i_index, (find(J==j)))=[A((find((A(:,1)==i & A(:,2)==j))),3)];
>   endfor;
> endfor;

Hope this helps,

James Sherman

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: for loop on decimal numbers fails.

Swiss_Knight
Hi,
thanks for the fast reply, and yes, it was a typo, sorry.

Nice trick, it helps, but I still have a strange error :

A=[sort(repmat ([1:1:10]',[4,1])), repmat([5:5:20]',[10;1]), rand(40,1)];
A(:,1)=A(:,1)/10;
A(:,2)=A(:,2)/10;

I=0.1:0.1:1;
J=0.5:0.5:2;

for i_index=1:length(I);
  i=I(i_index);
  for j_index=1:length(J);
    j=J(j_index);
    A((find((A(:,1)==i))))
  endfor;
endfor;

It fails for i=0.3 and i=0.7. (seems like a numerical computing issue ?!)

But if I define A this way with having the two first columns directly in the right units :
A=[sort(repmat ([0.1:0.1:1]',[4,1])), repmat([0.5 1 1.5 2]',[10;1]), rand(40,1)];

Everything is OK.
I can't understand...


Thanks a lot.
Reply | Threaded
Open this post in threaded view
|

Re: for loop on decimal numbers fails.

Colin Macdonald-2
In reply to this post by Swiss_Knight
On 19/07/16 09:59, Swiss_Knight wrote:
> if we loop on values which are not integers, it fails.

Possibly because of "floating point equality tests".

cheers,
Colin

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: for loop on decimal numbers fails.

Mike Miller-4
In reply to this post by Swiss_Knight
On Tue, Jul 19, 2016 at 14:34:33 -0700, Swiss_Knight wrote:

> Hi,
> thanks for the fast reply, and yes, it was a typo, sorry.
>
> Nice trick, it helps, but I still have a strange error :
>
> A=[sort(repmat ([1:1:10]',[4,1])), repmat([5:5:20]',[10;1]), rand(40,1)];
> A(:,1)=A(:,1)/10;
> A(:,2)=A(:,2)/10;
>
> I=0.1:0.1:1;
> J=0.5:0.5:2;
>
> for i_index=1:length(I);
>   i=I(i_index);
>   for j_index=1:length(J);
>     j=J(j_index);
>     A((find((A(:,1)==i))))
>   endfor;
> endfor;
>
> It fails for i=0.3 and i=0.7. (seems like a numerical computing issue ?!)

Yes. Octave does not compute numbers in arbitrary precision, and the
numbers [1:4, 6:9]./10 are not exactly representable. This is one of our
frequently asked questions on this list:

http://wiki.octave.org/FAQ#Why_is_this_floating_point_computation_wrong.3F

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave