Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: problem with Delete[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58948] Re: problem with Delete[]
  • From: Peter Pein <petsie at dordos.net>
  • Date: Sun, 24 Jul 2005 01:22:09 -0400 (EDT)
  • References: <dbt3vk$sn3$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Heath Gerhardt schrieb:
> Hello,
> 
> Delete[] is not removing elements of v in the following function, could 
> anyone tell me why?
> 
> Rn[N_, p_] := Module[{A, v, c, q, i, j},
>      A = Table[0, {i, 1, N}, {j, 1, N}];
>      v = Table[i, {i, 2, N}];
>      c = 1;
>      For[i = 1, i < N, i++,
>        j = Random[Integer, {1, Dimensions[v][[1]]}];
>        A[[c, v[[j]]]] = 1;
>        A[[v[[j]], c]] = 1;
>        q = Random[];
>        If[q > p, c = v[[j]]];
>        Delete[v, j];
>        ];
>      A
>      ]
> 
> thanks in advance,
> Heath
> 
Hi,

you forgot to assign the new (shorter) vector to v:
...
v=Delete[v,j]
];A]

========================

If you have to generate large matrices, consider another approach:

Pn[N_, p_] := Module[{a = {}, c = 1},
   Fold[(a = {a, c, #1[[#2]]};
        If[Random[] > p, c = #1[[#2]]];
        Delete[#1, #2]) & ,
      Range[2, N],
      Random[Integer, {1, #1}]& /@ Range[N - 1, 1, -1]];
     a = Union[Partition[Join[#1, Reverse[#1]]&[Flatten[a]], 2]];
     Normal[SparseArray[(#1 -> 1 & ) /@ a, {N, N}]]];

or omit the call to Normal to use SparseArrays (and about 0.1% of the
RAM used by the normalized array).

The version using For[] needs about Pi seconds ;-) for a 5555x5555 matrix:

Timing[Rn[5555, 0.5555];]
>>>{3.141*Second, Null}

The version using Fold[] and Map[] ("/@") needs for the same task:

Timing[Pn[5555, 0.5555];]
>>>{0.313*Second, Null}
-- 
Peter Pein
Berlin


  • Prev by Date: Re: limit problem
  • Next by Date: Re: limit problem
  • Previous by thread: Re: rotate frametick labels
  • Next by thread: Re: problem with Delete[]