# Multiplying a certain column from a table with a function

I have the following list of matrices whose second entry I would like to multiply/divide with a function, say f, and store the resulting table as a new table. How should I go about doing that? Thanks

***Note: Don't know whether it matters or not. But the second entry values run from -1.2 to 1.2, while the function argument runs from 0 to 1.2.

P = Table[{i^2, j}, {i, 5}, {j, -1.2, 1.2, .2}];
Dimensions@P
f = Interpolation[Table[{ky, ky + 1000}, {ky, 0, 1.2, 0.003}],
InterpolationOrder -> 0];


• whose second column second column? Did you look at the dimensions of P? Typing Dimensions[P] gives {10, 13, 2}. This is not a matrix, it is list of matrices. Are you sure this is what you want? Jun 5 at 6:16
• Yes, this is exactly what I want. Regards Jun 5 at 6:21
• to be more clear, I want the function scaling to be applied to the second column, that is, the count which says 13 in the dimensions list Jun 5 at 6:30
• For many reasons it would have been better to provide a minimal working example with the expected result and then the question would be to try and automate it for large lists of lists.
– bmf
Jun 5 at 8:18
• You are right. Just added a simple example of what I actually want Jun 5 at 9:07

This is too long to be left as a comment. Essentially, I am elaborating a bit more on what @Nasser pointed out.

With the definitions in the OP

P = Table[{i^2, j}, {i, 10}, {j, -1.2, 1.2, .2}];
f = Interpolation[Table[{ky, Cos[ky]}, {ky, 0, 1.2, 0.003}],
InterpolationOrder -> 0];


when we do

P // Dimensions


we get

{10, 13, 2}

What does this mean?

Well, if you do

P[[1]]


you get

{{1, -1.2}, {1, -1.}, {1, -0.8}, {1, -0.6}, {1, -0.4}, {1, -0.2}, {1, 2.22045*10^-16}, {1, 0.2}, {1, 0.4}, {1, 0.6}, {1, 0.8}, {1, 1.}, {1, 1.2}}

and you have 10 of those things.

If you do

P[[1]] // Dimensions


you get

{13,2}

which tells you that in the list P[[1]] you have 13 doublets; by doublet I mean a list of two elements.

So, it seems from the various comments that you want to multiply, divide, whatever, the y component in each case.

So, first try to understand how to extract those things.

For example, focus on the first one. Try to get all the second elements from all sublists that are contained in P[[1]]. The way to do it is

P[[1, All, 2]]


{-1.2, -1., -0.8, -0.6, -0.4, -0.2, 2.22045*10^-16, 0.2, 0.4, 0.6, 0.8, 1., 1.2}

Ok, now do whatever you want on this and recompose the list like so:

Transpose[{P[[1, All, 1]], f*P[[1, All, 2]]}]


The above has dimensions

{13, 2}


It's easy to see that in the first slot you have the original element and in the second slot the scaled one.

Then you can proceed and finish the big list of lists.

So, for instance

Table[Transpose[{P[[i, All, 1]], f*P[[i, All, 2]]}], {i, 1, Length@P}]


Is this what you wanted?

• Do you mean f@P[[i, All, 2]] rather than f*P[[i, All, 2]]? Jun 5 at 12:27
• @bbgodfrey no, I don't think so. f is a function that multiplies some elements. If you take what I have suggested with the toy model that was presented in the edited version of the question perhaps it will be clearer
– bmf
Jun 5 at 12:38
• Works perfectly. Thanks Jun 6 at 5:17

Try this:

 res=Transpose@{P[[All,1]],f/@P[[All,2]]}

• No it does not work unfortunately. It reduces the dimensions to {10,2,2} Jun 5 at 7:47