I have a table with coordinates {{x1,y1},{x2,y2}} that I want to multiply with a separate constant for x and one for y so: {{a,b},{a,b}}. (basically I rasterize a set of lines and then calculate the lines back) I have tried most of the suggestions that I found here on this forum and in the Documentation Center, but I cannot seem to find a correct and elegant solution. In (test) code:

edge = 5 
vxstep = 0.01 
voffsetbottom = 10 
vspace = 5 
vystep = 0.0705 
vlength = 100 
vnumcell = 6
myList = Table[{
    {Round[vedge/vxstep], Round[(voffsetbottom + ii*vspace)/vystep]}, 
    {Round[(vlength - vedge)/vxstep], Round[(voffsetbottom +ii*vspace)/vystep]}
   }, {ii, 1, vnumcell}];
myList // TableForm  
myList *= {{vxstep, vystep},{vxstep,vystep}} (*this is what I'd like, but does not work*)`

Does anyone have suggestions how to make this multiplication work? Thanks!

  • $\begingroup$ First you should post your code in Mathematica notations to simplify our work to help you.I suggest that you take a tour and have a look at the rules. Second, it is not quite clear what do you want to obtain. Starting with {{x1,y1},{x2,y2}} do you have in mind to get {{a*x1,b*y1},{a*x2,b*y2}}? $\endgroup$ – Alexei Boulbitch Jan 13 '16 at 15:04
  • $\begingroup$ Map[Times[#, {a, b}] &, {{x1, y1}, {x2, y2}}, {-2}]? $\endgroup$ – Algohi Jan 13 '16 at 15:23
  • $\begingroup$ {{a*x1,b*y1},{a*x2,b*y2}} is indeed wat I want to achieve. By te way, the code above is mathematica code, Except for the last line it runs perfectly. I'll give your suggestion a try! $\endgroup$ – Marc Koetse Jan 13 '16 at 15:27

A simple solution would be

myList = Map[# {vxstep, vystep} &, myList, {2}]

enter image description here

  • $\begingroup$ Its actually a tad faster to map at the top level: Map[# {{vxstep, vystep}, {vxstep, vystep}} &, myList, 1] $\endgroup$ – george2079 Jan 13 '16 at 17:01

Thanks to all of you. In the end I had to tweak the code a little to:

myList := Map[# {vxstep, vystep} &, myList, {2}]

in order to avoid protected tag issues in a dynamic module. But the script is running smoothly now!


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.