3

So I need to make a plot of some ~2000 data points I have from a spreadsheet. I'm able to import the data just fine, it stores it like so

sundat

{{280.,0.082},{280.5,0.099},......{3995.,0.0087},{4000.,0.00868}}

And I can call individual data sets or points like so

sundat[[1]]

{280.,0.082}

sundat[[1,1]]

280.

There are 2002 sets of two points of data. Now I can plot it fine using ListPlot, but the raw data I have is in messy units that I want to convert to mks units. I have the conversion factor for each column, but I have no idea how to do this. I presume I need to make a new list to apply this to, but trying something like

data2 = {{sundat[[All,1]]*(conversion1)},{sundat[[All,2]]*(conversion2)}} 

just totally messes up the table.

Any help would be appreciated.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
Amara
  • 33
  • 3

4 Answers4

8
sundat = {{280., 0.082}, {280.5, 0.099}, {3995., 0.0087}, {4000., 0.00868}};
conversionfactors = {2., .5};

data2 = Transpose[conversionfactors Transpose[sundat]]

or

data2 = conversionfactors # & /@ sundat

both give

(* {{560.,0.041},{561.,0.0495},{7990.,0.00435},{8000.,0.00434}} *)
kglr
  • 394,356
  • 18
  • 477
  • 896
6

You can also use Dot:

sundat.{{2., 0}, {0, .5}}

(*  {{560., 0.041}, {561., 0.0495}, {7990., 0.00435}, {8000., 0.00434}}  *)
Simon Woods
  • 84,945
  • 8
  • 175
  • 324
  • 1
    For some reason I always forget this form, though I have been shown it and even used it myself (e.g. (38180)). I don't like the theoretically wasted operations, but it's so well optimized it is still often faster than alternatives. – Mr.Wizard Sep 17 '14 at 20:17
2

Since Version 13.1 there is Threaded:

sundat = {{280., 0.082}, {280.5, 0.099}, {3995., 0.0087}, {4000., 0.00868}};

sundat * Threaded[{2., .5}]

{{560., 0.041}, {561., 0.0495}, {7990., 0.00435}, {8000., 0.00434}}

eldo
  • 67,911
  • 5
  • 60
  • 168
1

Although I prefer kguler's double Transpose you could also use Inner:

sundat = {{280., 0.082}, {280.5, 0.099}, {3995., 0.0087}, {4000., 0.00868}};

Inner[Times, sundat, {2., .5}, List]
{{560., 0.041}, {561., 0.0495}, {7990., 0.00435}, {8000., 0.00434}}

Or leveraging Simon's solution from: How can I make threading more flexible?

smartThread[sundat {2., .5}]
{{560., 0.041}, {561., 0.0495}, {7990., 0.00435}, {8000., 0.00434}}

A related question: Mapping a function over the parts of a deeply nested Array

See also: Elegant operations on matrix rows and columns

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371