help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: for loop on decimal numbers fails.


From: James Sherman Jr.
Subject: Re: for loop on decimal numbers fails.
Date: Tue, 19 Jul 2016 16:56:08 -0400

On Tue, Jul 19, 2016 at 12:59 PM, Swiss_Knight <address@hidden> 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
> address@hidden
> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]