Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.6K Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Multiply two lists with different corresponding values

Posted 2 years ago

I want to multiply the elements of c1 (with corresponding values t1) with the matching elements of c2 (corresponding to the values of t2).

For example, for

t1 = {1, 2, 3, 4, 5, 6, 7}
c1 = {12, 19, 34, 48, 35, 40, 47}
t2 = {2, 4, 6}
c2 = {21, 45, 17}

I want to get

{19*21,48*45,40*17}

Any idea for doing it?

POSTED BY: Ehud Behar
3 Replies

Another way:

c1[[t2]]*c2
POSTED BY: Gianluca Gorni
Posted 2 years ago

Yes, that works in Ehud's example. But not if t1 is a permutation of Range@Length@c1

t1 = {1, 5, 2, 7, 4, 3, 6};
c1 = {12, 19, 34, 48, 35, 40, 47};
t2 = {5, 7, 3};
c2 = {21, 45, 17};

Pick[c1, t1, n_ /; MemberQ[t2, n]]*c2
c1[[t2]]*c2
POSTED BY: Hans Milton
Posted 2 years ago

One way using Pick:

t1={1,2,3,4,5,6,7};
c1={12,19,34,48,35,40,47};
t2={2,4,6};
c2={21,45,17};

Pick[c1,t1,n_/;MemberQ[t2,n]]*c2
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard