Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.9K Views
|
10 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Multiply a number to the second item in a list?

Posted 4 years ago

This is my code:

In[62]:= Pbranch = Table[{-x + 21, x}, {x, 10, 1, -1}]

Out[62]= {{11, 10}, {12, 9}, {13, 8}, {14, 7}, {15, 6}, {16, 5}, {17, 
  4}, {18, 3}, {19, 2}, {20, 1}}

In[169]:= NewPbranch = Pbranch*PBoltz

Out[169]= {{19.2527, 17.5025}, {29.8551, 22.3913}, {43.457, 
  26.7427}, {59.272, 29.636}, {75.5105, 30.2042}, {89.3732, 
  27.9291}, {97.3841, 22.9139}, {96.0834, 16.0139}, {82.9588, 
  8.73251}, {57.3497, 2.86748}}

I want PBoltz to only multiply the second number in each set. I just want PBoltz to multiply 10, 9, 8, 7, and so forth. PBoltz is another list of numbers.

POSTED BY: Garrett Miskell
10 Replies
Posted 4 years ago

Another way using Hans's example data

list = {{11, 10}, {12, 9}, {13, 8}, {14, 7}, {15, 6}, {16, 5}, {17, 4}, {18, 3}, {19, 2}, {20, 1}}
pboltz = Table[x^j, {j, 10}]
MapThread[{First@#1, Last@#1*#2} &, {list, pboltz}]
POSTED BY: Rohit Namjoshi

It seems that MapThread, although coool, is not necessary:

list = {{11, 10}, {12, 9}, {13, 8}, {14, 7}, {15, 6}, {16, 5}, {17,    4}, {18, 3}, {19, 2}, {20, 1}}
pboltz = Table[x^j, {j, 10}]
Thread[g[list, pboltz]] /. g -> Function[{a, b}, {a[[1]], a[[2]] b}]

Inserting the Function direct into the Thread doesn't work, because it is evaluated before Thread.

@Rohit: is there a way (other than MapThread or the rule above) to delay the evaluation of the Function?

POSTED BY: Hans Dolhaine

Well, there are lots of explanations and examples in the "Help" section of your Mathematica. reading around and experimenting would be good.

POSTED BY: Hans Dolhaine

Do you mean something like this?

list = {{11, 10}, {12, 9}, {13, 8}, {14, 7}, {15, 6}, {16, 5}, {17, 4}, {18, 3}, {19, 2}, {20, 1}}
pboltz = Table[x^j, {j, 10}]
Transpose[{Transpose[list][[1]], Transpose[list][[2]] pboltz}]
POSTED BY: Hans Dolhaine
Posted 4 years ago

EDITED TO FIX TYPO.

Check out Map[]. Then you can do something like this:

Map[
 {#[[1]], 2 * #[[2]]} &,
 Pbranch
 ]
POSTED BY: Mike Besso

Thank you.

POSTED BY: Hans Dolhaine
Posted 4 years ago

Hi Hans,

Unfortunately, there is no easy way. I ran into this issue a while back and this MSE post has some workarounds.

POSTED BY: Rohit Namjoshi
Posted 4 years ago

@Garrett: My apologies, I had a typo. The second [[1]] should have been [[2]]. I have updated my response.

POSTED BY: Mike Besso

Yes! Thank you so much!! I'm relatively new to Mathematica. Do you have any recommendations where I could learn stuff like this?

POSTED BY: Garrett Miskell

Thank you so much for replying! That helps a little, but since PBoltz is also a list, it multiplies the second number in Pbranch by every number in PBoltz. Do you know how I can make it so that the first number in PBoltz multiplies only the the second number in my first set, and then the second number of PBoltz multiplies the second number in the second set of PBranch. I'll copy/paste what I mean.

In[226]:= Map[{#[[1]], PBoltz*#[[1]]} &, Pbranch]

Out[226]= {{11, {19.2527, 27.3672, 36.7713, 46.5708, 55.3743, 61.4441,
    63.0132, 58.7176, 48.0288, 31.5423}}, {12, {21.003, 29.8551, 
   40.1141, 50.8045, 60.4084, 67.0299, 68.7417, 64.0556, 52.395, 
   34.4098}}, {13, {22.7532, 32.343, 43.457, 55.0382, 65.4424, 
   72.6157, 74.4702, 69.3936, 56.7613, 37.2773}}, {14, {24.5035, 
   34.831, 46.7998, 59.272, 70.4764, 78.2015, 80.1986, 74.7315, 
   61.1275, 40.1448}}, {15, {26.2537, 37.3189, 50.1427, 63.5057, 
   75.5105, 83.7874, 85.9271, 80.0695, 65.4938, 
   43.0122}}, {16, {28.004, 39.8068, 53.4855, 67.7394, 80.5445, 
   89.3732, 91.6556, 85.4075, 69.8601, 45.8797}}, {17, {29.7542, 
   42.2947, 56.8283, 71.9731, 85.5785, 94.959, 97.3841, 90.7454, 
   74.2263, 48.7472}}, {18, {31.5045, 44.7827, 60.1712, 76.2068, 
   90.6125, 100.545, 103.113, 96.0834, 78.5926, 
   51.6147}}, {19, {33.2547, 47.2706, 63.514, 80.4405, 95.6466, 
   106.131, 108.841, 101.421, 82.9588, 54.4822}}, {20, {35.005, 
   49.7585, 66.8569, 84.6742, 100.681, 111.716, 114.569, 106.759, 
   87.3251, 57.3497}}}
POSTED BY: Garrett Miskell
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard