5
$\begingroup$

I need to use the interpolation function on some data so I can integrate it. I have an array with 1400 subsets and there are 1400 elements in each subset: { {1} {2}....{1400}} (Sorry, I don't know any of the right terminology for arrays). Each element represents a z value of a graph.

To make it simpler to explain I'll use a short version of my array:

{{1,2,3,4,5} , {5,6,7,8,9} , {9,10,11,12,13}}

The first element is the z value for coordinates (1,1), the second element is at (1,2) the third at (1,3) and so on. The first element of the second group is at (2,1), the second is at (2,2) and so on. Somehow, I need to create something of the form

{{{x1,y1},z1}, {{x2,y2},z2}...}

I have absolutely no idea how to go about it. So for my example array, I'd want the code to generate: {{{1,1},1}, {{1,2},2}, {{1,3},3}, ...{{3,5},13}} which the interpolation function would then work on. Any help or suggestions would be hugely appreciated, thanks!

$\endgroup$
  • $\begingroup$ I suggest you to format this question properly, as the admins could hold it for this reason. $\endgroup$ – Riccardo Cazzin Jan 2 '18 at 18:52
5
$\begingroup$

I assume your data is square matrix. Is this what you are looking for?

  data = Range@25~Partition~5

{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25}}

 Table[{i, j, data[[i, j]]}, {i, Length@data}, {j,Length@data}]

Or equivalently

Flatten@Riffle[Tuples[Range@Length@data, 2], Flatten@data]~Partition~3

{{{1, 1, 1}, {1, 2, 2}, {1, 3, 3}, {1, 4, 4}, {1, 5, 5}}, {{2, 1, 6}, {2, 2, 7}, {2, 3, 8}, {2, 4, 9}, {2, 5, 10}}, {{3, 1, 11}, {3, 2, 12}, {3, 3, 13}, {3, 4, 14}, {3, 5, 15}}, {{4, 1, 16}, {4, 2, 17}, {4, 3, 18}, {4, 4, 19}, {4, 5, 20}}, {{5, 1, 21}, {5, 2, 22}, {5, 3, 23}, {5, 4, 24}, {5, 5, 25}}}

Or maybe this is what you want.

Catenate@Table[{{i, j}, data[[i, j]]}, {i, Length@data}, {j,Length@data}]

{{{1, 1}, 1}, {{1, 2}, 2}, {{1, 3}, 3}, {{1, 4}, 4}, {{1, 5}, 5}, {{2, 1}, 6}, {{2, 2}, 7}, {{2, 3}, 8}, {{2, 4}, 9}, {{2, 5}, 10}, {{3, 1}, 11}, {{3, 2}, 12}, {{3, 3}, 13}, {{3, 4}, 14}, {{3, 5}, 15}, {{4, 1}, 16}, {{4, 2}, 17}, {{4, 3}, 18}, {{4, 4}, 19}, {{4, 5}, 20}, {{5, 1}, 21}, {{5, 2}, 22}, {{5, 3}, 23}, {{5, 4}, 24}, {{5, 5}, 25}}

Here is the different way to do it

  Riffle[Tuples[Range@Length@data, 2], Flatten@data]~Partition~2

{{{1, 1}, 1}, {{1, 2}, 2}, {{1, 3}, 3}, {{1, 4}, 4}, {{1, 5}, 5}, {{2, 1}, 6}, {{2, 2}, 7}, {{2, 3}, 8}, {{2, 4}, 9}, {{2, 5}, 10}, {{3, 1}, 11}, {{3, 2}, 12}, {{3, 3}, 13}, {{3, 4}, 14}, {{3, 5}, 15}, {{4, 1}, 16}, {{4, 2}, 17}, {{4, 3}, 18}, {{4, 4}, 19}, {{4, 5}, 20}, {{5, 1}, 21}, {{5, 2}, 22}, {{5, 3}, 23}, {{5, 4}, 24}, {{5, 5}, 25}}

$\endgroup$
  • $\begingroup$ The second one gave me exactly what I was after! Thank you so much, I was not getting anywhere with that! $\endgroup$ – user13948 Jan 2 '18 at 22:15
6
$\begingroup$
array = {{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}};

Join @@ MapIndexed[{#2, #} &, array, {2}]

{{{1, 1}, 1}, {{1, 2}, 2}, {{1, 3}, 3}, {{1, 4}, 4}, {{1, 5}, 5},
{{2, 1}, 5}, {{2, 2}, 6}, {{2, 3}, 7}, {{2, 4}, 8}, {{2, 5}, 9},
{{3, 1}, 9}, {{3, 2}, 10}, {{3, 3}, 11}, {{3, 4}, 12}, {{3, 5}, 13}}

$\endgroup$
2
$\begingroup$
array = {{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}}; 

Transpose[{
   Flatten[Outer[List, Range@Length@array,Range@Length@array[[1]]], 1], 
   # & /@ (Flatten@array)
}]

{{{1, 1}, 1}, {{1, 2}, 2}, {{1, 3}, 3}, {{1, 4}, 4}, {{1, 5}, 5}, 
{{2, 1}, 5}, {{2, 2}, 6}, {{2, 3}, 7}, {{2, 4}, 8}, {{2, 5}, 9}, 
{{3, 1}, 9}, {{3, 2}, 10}, {{3, 3}, 11}, {{3, 4}, 12}, {{3, 5}, 13}}
$\endgroup$
2
$\begingroup$
array // ArrayRules // Most // Association // KeyValueMap[List, #] &

or (original answer):

List @@@ Most@ArrayRules@array

{{{1, 1}, 1}, {{1, 2}, 2}, {{1, 3}, 3}, {{1, 4}, 4}, {{1, 5}, 5}, {{2, 1}, 5}, {{2, 2}, 6}, {{2, 3}, 7}, {{2, 4}, 8}, {{2, 5}, 9}, {{3, 1}, 9}, {{3, 2}, 10}, {{3, 3}, 11}, {{3, 4}, 12}, {{3, 5}, 13}}

where (after kglr's answer)

array = {{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}};
$\endgroup$

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.