2
A = {a,b,c,d,e,f,g,h}
B = {1,2,3,4,5}

How would I go about setting some of the elements in list A equal to the elements in list B and then the left over elements in list A will be set to 0. For example, set a=1, c=2, d=3, g=4, h=5 and then the leftover elements in list A are b,e,f=0. So the new list A would be

A = {1,0,2,3,0,0,4,5}
Johnny
  • 71
  • 2

6 Answers6

4

You didn't specify what your input should be so I have to guess. Perhaps:

A = {a, b, c, d, e, f, g, h};
B = {1, 2, 3, 4, 5};
pos = {1, 3, 4, 7, 8};

A[[All]] = 0;
A[[pos]] = B;

A
{1, 0, 2, 3, 0, 0, 4, 5}

You could also start with this for a bit more efficiency:

A = ConstantArray[0, Length @ A]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • I think that does not answer the right question: the OP wants to assign values to the symbols appearing in A. – Igor Rivin Sep 18 '14 at 16:50
  • @IgorRivin my interpretation is that he was merely using that notation to describe the replacements. If not you win. :-) – Mr.Wizard Sep 18 '14 at 18:50
3
 const = ConstantArray[0, Length[A]]
 const[[pos]] = B
 MapThread[Set, {A, const}]
Karsten7
  • 27,448
  • 5
  • 73
  • 134
Igor Rivin
  • 5,094
  • 20
  • 19
2
A = {a, b, c, d, e, f, g, h};
B = {1, 2, 3, 4, 5};
pos = {1, 3, 4, 7, 8};

aA = SparseArray[pos -> B];
aA // Normal
(* {1,0,2,3,0,0,4,5} *)

More generally,

(* replace positions # of #3 with #2 and the rest with #4 *)
foo = SparseArray[# -> #2, Length @ #3, #4]& 
foo[pos, B, A, 0] //Normal
(* {1, 0, 2, 3, 0, 0, 4, 5} *)
foo[pos, B, A, 100] // Normal (* use 100 instead of 0 as the default element *)
(* {1, 100, 2, 3, 100, 100, 4, 5} *)
foo[{2, 3, 4, 7}, {1, 2, 3, 4}, A, 100] // Normal (* replace any part of A *)
(* {100, 1, 2, 3, 100, 100, 4, 100} *)    

Update: Few more alternatives:

ReplacePart[0 A, Thread[pos -> B]]
(* {1, 0, 2, 3, 0, 0, 4, 5} *)
Block[{i = 1}, MapAt[B[[i++]] &, 0 A, List /@ pos]]
(* {1, 0, 2, 3, 0, 0, 4, 5} *)
kglr
  • 394,356
  • 18
  • 477
  • 896
  • As written this is very clean, but if the default element is not 0, and if you are not replacing the last element in A, you will need additional parameters, e.g. SparseArray[pos -> B, Length @ A, bg] where bg is the default. +1 but I suggest you include these caveats in the answer. – Mr.Wizard Sep 18 '14 at 18:49
  • 1
    @Mr.W, good point; thank you. Updated with your suggestions. – kglr Sep 18 '14 at 19:23
2

Just for fun:

A = {a, b, c, d, e, f, g, h};
B = {1, 2, 3, 4, 5};
p = {1, 3, 4, 7, 8};

Then:

(A = ConstantArray[0, Length @ A]; 
 Scan[(A[[p[[#]]]] = B[[#]]) &, Range @ Length @ pos]; A)
{1, 0, 2, 3, 0, 0, 4, 5}
RunnyKine
  • 33,088
  • 3
  • 109
  • 176
1

Also:

A = {a, b, c, d, e, f, g, h};
B = {1, 2, 3, 4, 5};
Replace[A, {a -> 2, b -> 5, _ -> 0}, 1]

(* {2, 5, 0, 0, 0, 0, 0, 0} *)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
0
A = {a, b, c, d, e, f, g, h} ;
{a, c, d, g, h} = {1, 2, 3, 4, 5};
Replace[A, _Symbol :> 0, {-1}]
(*{1, 0, 2, 3, 0, 0, 4, 5}*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78