5
$\begingroup$

I have a list of list

A= {{0.620161, 0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177, 
  1.13142, 1.15048, 1.23244, 0.721388, 0.708943, 0.750067, 0.744916, 
  0.720972, 0.674833, 1.29773, 1.29514}, {0.620161, 0.320312, 0.94842,
   1.11844, 1.12045, 1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 
  0.750067, 0.744916, 0.720972, 0.674833, 1.29383, 
  1.29514}, {0.620161, 0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 
  1.13177, 1.13142, 1.15048, 0.721388, 0.750067, 0.744916, 0.720972, 
  0.674833, 1.29383, 1.29514}, {0.620161, 0.320312, 0.94842, 1.11844, 
  1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 0.750067, 0.744916, 
  0.720972, 0.674833, 1.29383, 1.29514}, .... }}.

The number of lists inside is not known. I want to replace all the elements of the first list by 20, all the elements of the second list by 20.1 and so on for all the lists.

I tried ReplaceList but with no success, can anyone help me with this?

$\endgroup$
  • $\begingroup$ You can start with list[[1,;;]]=20. It should be easy to generalize this in the way you need it. $\endgroup$ – Gregory Rut Oct 11 '14 at 7:47
  • $\begingroup$ Yes, I know how to do list by list, but I don't know how to generalize or do it automatically for all the lists. Even the resources on Wolfram don't say most of the stuff that are discussed here. $\endgroup$ – HuShu Oct 11 '14 at 8:10
6
$\begingroup$
ClearAll[miF, rPF, tBF];
miF = MapIndexed[20 + .1 (#2[[1]] - 1) &, #, {2}] &;

or

rPF = ReplacePart[#, {i_, _} :> (19.9 + 0.1 i)] &;

or

tbF = Block[{j = 0}, Table[ 0 i + 20 + 0.1 j++, {i, #}]] &;

Example:

A = {{0.620161, 0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177,
     1.13142, 1.15048, 1.23244, 0.721388, 0.708943, 0.750067, 
    0.744916, 0.720972, 0.674833, 1.29773, 1.29514}, {0.620161, 
    0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177, 1.13142, 
    1.15048, 0.721388, 0.750067, 0.744916, 0.720972, 0.674833, 
    1.29383, 1.29514}, {0.620161, 0.320312, 0.94842, 1.11844, 1.12045,
     1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 0.750067, 0.744916,
     0.720972, 0.674833, 1.29383, 1.29514}, {0.620161, 0.320312, 
    0.94842, 1.11844, 1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 
    0.750067, 0.744916, 0.720972, 0.674833, 1.29383, 1.29514}};

miF@A
(* {{20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20.},
    {20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1,  20.1, 20.1, 20.1, 20.1, 20.1,
     20.1, 20.1, 20.1, 20.1}, 
    {20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2,
     20.2, 20.2, 20.2, 20.2}, 
    {20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3,
     20.3, 20.3, 20.3}} *)

miF @ A == rPF @ A == tbF @ A
(* True  *)
$\endgroup$
  • $\begingroup$ To a person who has never programmed before, this seems like magic. Thank you. $\endgroup$ – HuShu Oct 11 '14 at 8:08
  • $\begingroup$ @Nilanjan, my pleasure. Welcome to Mathematica.SE. $\endgroup$ – kglr Oct 11 '14 at 8:22
  • 2
    $\begingroup$ This is a rather inefficient method as you apply the function to every element rather than using a vector operation. $\endgroup$ – Mr.Wizard Oct 12 '14 at 2:19
11
$\begingroup$

Another interesting approach:

i = 0;
Map[0 # + (20 + 0.1 i++) &, A]
{{20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 
  20., 20., 20., 20., 20.}, {20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1,
   20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1}, {20.2, 20.2,
   20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 
  20.2, 20.2, 20.2}, {20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 
  20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3}}
$\endgroup$
  • 2
    $\begingroup$ The easiest approach, indeed $\endgroup$ – Dr. belisarius Oct 11 '14 at 14:41
5
$\begingroup$

Using:

A = {{0.620161, 0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177,
     1.13142, 1.15048, 1.23244, 0.721388, 0.708943, 0.750067, 
    0.744916, 0.720972, 0.674833, 1.29773, 1.29514}, {0.620161, 
    0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177, 1.13142, 
    1.15048, 0.721388, 0.750067, 0.744916, 0.720972, 0.674833, 
    1.29383, 1.29514}, {0.620161, 0.320312, 0.94842, 1.11844, 1.12045,
     1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 0.750067, 0.744916,
     0.720972, 0.674833, 1.29383, 1.29514}, {0.620161, 0.320312, 
    0.94842, 1.11844, 1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 
    0.750067, 0.744916, 0.720972, 0.674833, 1.29383, 1.29514}};

A function to replace list:

rep[a_, v_] := MapThread[Table[#2, {Length@#1}] &, {a, v}]

so

rep[A, 19.9 + Range[Length[A]]/10]

yields:

{{20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 
  20., 20., 20., 20., 20.}, {20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1,
   20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1}, {20.2, 20.2,
   20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 
  20.2, 20.2, 20.2}, {20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 
  20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3}}
$\endgroup$
  • $\begingroup$ ConstantArray would be better than Table here as the latter assumes a changing value. +1 nevertheless. $\endgroup$ – Mr.Wizard Oct 12 '14 at 2:18
4
$\begingroup$
Table[A[[i, All]] = Table[20. + n/10, {n, 0, Length[A] - 1}][[i]], {i, 1, Length[A]}];

A // TableForm

enter image description here

$\endgroup$
4
$\begingroup$

Borrowing from existing answers:

19.9 + Range@Length@A/10 + 0 A
$\endgroup$
  • $\begingroup$ Why is it oftentimes so difficult to see the simple things? $\endgroup$ – eldo Oct 12 '14 at 15:15
2
$\begingroup$

Two more:

19.9 + #1/10 + 0 #2 & @@@ Transpose@{Range@Length@A, A}

or

Transpose@{Range@Length@A, A} /. {x_, y_} :> 0 y + 19.9 + x/10
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.