MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Array from for-loop iterations

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116891] Re: Array from for-loop iterations
  • From: Peter Pein <petsie at dordos.net>
  • Date: Thu, 3 Mar 2011 05:59:57 -0500 (EST)
  • References: <ikl2vv$sqd$1@smc.vnet.net>

Am 02.03.2011 10:34, schrieb Lengyel Tamas:
> Dear Mathworld Community,
>
> I am tackling a simple problem,
>
> I have written a simple for-loop using elements from an array of
> 3-dimensional vectors:
>
> f0=0.5;
> For[k = 1, k<  9, k++,
>
>   (vec[[k, 3]] - f0)^2 + (vec[[k, 1]] - f0) (vec[[k, 2]] - vec[[k, 1]]) -
> vec[[k, 2]] - f0) (vec[[k, 3]] - f0)
>      ]
>
> I don't know how to make an array out of the computed results.
>
> Please help my silly soul.
>
> Thank you.
>
> Tamás
>

Hi,

use Table in place of For:

In[1]:= vec = RandomReal[{-1, 1}, {9, 3}]; (* build some data *)
f0 = 0.5;
Table[
     (vec[[k,3]] - f0)^2 + (vec[[k,1]] - f0)*
     (vec[[k,2]] - vec[[k,1]]) - (vec[[k,2]] - f0)*
     (vec[[k,3]] - f0),
  {k, 1, 9}]
Out[3]= 
{1.02115,0.842108,0.207395,-0.0851334,0.791745,0.52546,-2.59654,0.334137,-1.64334}

hth,
Peter


  • Prev by Date: Re: How to suppress (hide) RunThrough window under Windows?
  • Next by Date: Re: How to suppress (hide) RunThrough window under Windows?
  • Previous by thread: Re: Array from for-loop iterations
  • Next by thread: Re: Array from for-loop iterations