2
$\begingroup$
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}
$\endgroup$
4
$\begingroup$

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]
$\endgroup$
2
  • $\begingroup$ I think that does not answer the right question: the OP wants to assign values to the symbols appearing in A. $\endgroup$
    – Igor Rivin
    Sep 18 '14 at 16:50
  • $\begingroup$ @IgorRivin my interpretation is that he was merely using that notation to describe the replacements. If not you win. :-) $\endgroup$
    – Mr.Wizard
    Sep 18 '14 at 18:50
3
$\begingroup$
 const = ConstantArray[0, Length[A]]
 const[[pos]] = B
 MapThread[Set, {A, const}]
$\endgroup$
4
  • $\begingroup$ Why a downvote? $\endgroup$
    – Igor Rivin
    Sep 18 '14 at 17:45
  • 1
    $\begingroup$ It wasn't me that downvoted, but I would guess it's because C is Protected, and C[pos] should have been C[[pos]]. Also Set by itself is cleaner than Set[#1, #2]& $\endgroup$ Sep 18 '14 at 17:54
  • $\begingroup$ I too did not down-vote, but additionally this is making assignments to the symbols in the list A, which is unlike other interpretations. If that's actually what the OP wants then great. :-) $\endgroup$
    – Mr.Wizard
    Sep 18 '14 at 18:47
  • $\begingroup$ @SimonWoods Fixed. $\endgroup$
    – Igor Rivin
    Sep 18 '14 at 19:21
2
$\begingroup$

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}
$\endgroup$
2
$\begingroup$
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} *)
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Sep 18 '14 at 18:49
  • 1
    $\begingroup$ @Mr.W, good point; thank you. Updated with your suggestions. $\endgroup$
    – kglr
    Sep 18 '14 at 19:23
1
$\begingroup$

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} *)
$\endgroup$
0
$\begingroup$
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}*)
$\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.