1
$\begingroup$

I have an array of lists and want to correlate every list with each other like this:

Table[Table[Correlation[list[[y]], list[[x]]], {x, 1, Length[list]}], {y,1, Length[list]}];

I take the first list and correlate it with all others asf. I pretty much assume, this is not a very elegant and efficient code, so I wanted to ask for some alternatives. How would you guys do it?

$\endgroup$
2

3 Answers 3

4
$\begingroup$

It's not entirely clear to me what you mean by "browse through an array" but from my best guess you might try:

array = RandomInteger[9, {50, 50}];

Pane[TableForm @ array, {500, 400}, Scrollbars -> True]

enter image description here


Since this is not the meaning you intended here is another:

list = {a, b, c, d};

Outer[f, list, list]
{{f[a, a], f[a, b], f[a, c], f[a, d]},
 {f[b, a], f[b, b], f[b, c], f[b, d]},
 {f[c, a], f[c, b], f[c, c], f[c, d]},
 {f[d, a], f[d, b], f[d, c], f[d, d]}}
f @@@ Tuples[list, 2]
 {f[a, a], f[a, b], f[a, c], f[a, d], f[b, a], f[b, b], f[b, c], f[b, d],
  f[c, a], f[c, b], f[c, c], f[c, d], f[d, a], f[d, b], f[d, c], f[d, d]}

Note that Outer produces a table of values (nested lists) while Tuples produces a flat list.

See also: how to efficiently apply function to all pairs of a set (and collect the results)

$\endgroup$
7
  • 1
    $\begingroup$ or ArrayPlot[array, ColorFunction -> "Rainbow"] for the more visual people out there :) $\endgroup$ Commented Jan 30, 2014 at 16:39
  • $\begingroup$ @PinguinDirk Of course that would be better for numeric data; this is intended as a generic method, where elements are arbitrary expressions. $\endgroup$
    – Mr.Wizard
    Commented Jan 30, 2014 at 16:40
  • 1
    $\begingroup$ sure, just kidding - one can only guess what he wants, at this point $\endgroup$ Commented Jan 30, 2014 at 16:41
  • $\begingroup$ Sorry, don't know how to call it. I meant getting to every value of the array, e.g. for multiplying or correlating all values of an array with each other. $\endgroup$
    – holistic
    Commented Jan 30, 2014 at 16:47
  • 1
    $\begingroup$ @Mr.Wizard I think he means "browse" as in the Scan sense... walk through the list. What OP wants is just Outer[Correlation, list, list] (possibly fix levels... didn't try), which is most likely a millionth duplicate. $\endgroup$
    – rm -rf
    Commented Jan 30, 2014 at 17:13
4
$\begingroup$

Another option might be to use TableView This gives the row number and column number on the sides as well. This is undocumented still as of Version 9.01, but it is there.

Mathematica graphics

array = RandomInteger[9, {50, 50}];
TableView[array]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ I see they've improved TableView since v7; looks nice. +1 $\endgroup$
    – Mr.Wizard
    Commented Jan 31, 2014 at 6:52
0
$\begingroup$

I would do it like this:

Correlation @ Transpose @ list
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.