4
$\begingroup$

I have an array

data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

And I want map Standardize over the column of the array. I use following code

Composition[
  Transpose,
  (Standardize /@ #)&,
  Transpose][data]
{{-1, 0, 1}, {-1, 0, 1}, {-1, 0, 1}}

Is there a shorter way to get that result? PS: maybe using of MapThread, but function Standarize takes a list as input.

EDIT

I have picked a bad example. Use this example:

data = {{-4, -8, -8}, {-9, 4, 10}, {-3, -9, 0}}

The result I desire:

{{0.4147806779,-0.5068532453,-0.9609876522},{-1.140646864,1.151939194,1.034909779},{0.7258661863,-0.6450859486,-0.0739221271}}
$\endgroup$

2 Answers 2

7
$\begingroup$

How about this?

Standardize@data

I'll assume you wanted something more general, so eg

MapThread[f, data]

(*{f[-4, -9, -3], f[-8, 4, -9], f[-8, 10, 0]}*)

Or if f only accepts a list as a parameter, why, wrap the arguments inside a list:

MapThread[
 f[{##}] &,
 data
 ]
$\endgroup$
3
  • $\begingroup$ To generalize your approach just span to the end. data == Array[d, {5, 7}]; Standardize@data[[All, 1 ;;]] == Composition[Transpose, (Standardize /@ #) &, Transpose][data] True $\endgroup$
    – Bob Hanlon
    Jul 15, 2014 at 13:18
  • $\begingroup$ This code MapThread[f, data] assume that function take several parameters. But function must take a List. But in particular Standardize@data You are right $\endgroup$ Jul 15, 2014 at 13:22
  • $\begingroup$ well, here you go then $\endgroup$
    – acl
    Jul 15, 2014 at 13:24
2
$\begingroup$

This should work

Thread[Standardize[data]]

Edit after the question was changed and comment from acl:

Standardize /@ Thread[data] // Transpose
$\endgroup$
3
  • 2
    $\begingroup$ but don't they say they want to map f over the columns? and doesn't Thread[f[data]] map f over rows? $\endgroup$
    – acl
    Jul 15, 2014 at 13:21
  • $\begingroup$ @acl corrected this. It's not elegant. $\endgroup$
    – Karsten 7.
    Jul 15, 2014 at 13:35
  • $\begingroup$ @Karsten7 neither is mine, it seems the real problem they had wasn't the mapping bit. $\endgroup$
    – acl
    Jul 15, 2014 at 16:32

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.