[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Invalid index error
From: |
Geraint Paul Bevan |
Subject: |
Re: Invalid index error |
Date: |
Sun, 01 Aug 2004 23:28:08 +0100 |
User-agent: |
Mozilla Thunderbird 0.5 (X11/20040306) |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Rich Shepard wrote:
| On Sun, 1 Aug 2004, Geraint Paul Bevan wrote:
|
|
|>The final value of the loop counter i is 6, but you are referencing
|>element a1(i+1) and 6+1=7 which is beyond the end of the array.
|
|
| Sigh. Of course.
|
| What is the better way of writing this, then?
|
| Thanks,
|
| Rich
|
To use the for loop, you just need to change (i+1) to (i):
octave:1> a = rand(1,6)
a =
~ 0.81472 0.13548 0.90579 0.83501 0.12699 0.96887
octave:2> product = 1;
octave:3> for i = 1:6
| product = product * a(i)
| endfor
product = 0.81472
product = 0.11038
product = 0.099978
product = 0.083482
product = 0.010601
product = 0.010271
However, in this case, you can make use of Octave's "prod" function to
save yourself a little work:
octave:4> prod(a)
ans = 0.010271
- --
Geraint Bevan
http://homepage.ntlworld.com/geraint.bevan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iEYEARECAAYFAkENbngACgkQcXV3N50QmNM7jgCgglcD4HrrWRNqkBv2SkfRaZrv
IG4AoId+fXy03FYOoPT8ciSxib2uT2Jz
=vQ8r
-----END PGP SIGNATURE-----
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------
- Invalid index error, Rich Shepard, 2004/08/01
- Message not available
- Message not available
- Re: Invalid index error,
Geraint Paul Bevan <=
- Re: Invalid index error, Rich Shepard, 2004/08/01
- Re: Invalid index error, Geraint Paul Bevan, 2004/08/01
- Re: Invalid index error, Rich Shepard, 2004/08/01
- Re: Invalid index error, Geraint Paul Bevan, 2004/08/01
- Re: Invalid index error, Rich Shepard, 2004/08/01
- Re: Invalid index error, Geraint Paul Bevan, 2004/08/01