2
$\begingroup$

This question already has an answer here:

Given the matrix:

matrix = ConstantArray[0, {3, 2}]

and date list:

list = {a, b, c, d, e, f}

a way to enter the list elements in the matrix is as follow:

matrix[[1,1]] = list[[1]];
matrix[[1,2]] = list[[2]];
matrix[[2,1]] = list[[3]];
matrix[[2,2]] = list[[4]];
matrix[[3,1]] = list[[5]];
matrix[[3,2]] = list[[6]];

getting what you want:

matrix == {{a, b}, {c, d}, {e, f}}
(*True*)

You could show a more elegant and effective way to do this?

Thanks so much.


Similarly, I can write:

matrix = ConstantArray[0, {3, 2}];

list1 = {a, b, c};
list2 = {d, e, f};
list3 = {g, h, i};
list4 = {j, k, l};
list5 = {m, n, o};
list6 = {p, q, r};

list = {list1, list2, list3, list4, list5, list6};

matrix[[1, 1]] = list[[1, 1]];
matrix[[1, 2]] = list[[2, 1]];
matrix[[2, 1]] = list[[3, 1]];
matrix[[2, 2]] = list[[4, 1]];
matrix[[3, 1]] = list[[5, 1]];
matrix[[3, 2]] = list[[6, 1]];

and obtain:

matrix == {{a, d}, {g, j}, {m, p}}
(*True*)

but I can not take advantage of the command that you kindly showed me.

In this other case, how can you do?


One last thing, instead of writing:

matrix[[1, 1]] = Grid[list[[1, 1]], Frame -> All, ItemSize -> All];
matrix[[1, 2]] = Grid[list[[2, 1]], Frame -> All, ItemSize -> All];
...
matrix[[3, 2]] = Grid[list[[6, 1]], Frame -> All, ItemSize -> All];

how can I do? I thought I was able to generalize the previous simple cases but I can not.

$\endgroup$

marked as duplicate by Mr.Wizard list-manipulation Feb 13 '17 at 10:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ matrix = ArrayReshape[list, {3, 2}]? $\endgroup$ – Kuba Feb 12 '17 at 13:29
  • $\begingroup$ Fantastic, I did not know this command! Thank you! $\endgroup$ – TeM Feb 12 '17 at 13:30
  • $\begingroup$ Notice that you don't have to define matrix with ConstantArray before. Unless you want to fill part of a bigger matrix, but that is a different question. $\endgroup$ – Kuba Feb 12 '17 at 13:31
  • 2
    $\begingroup$ Partition[list, 2] $\endgroup$ – Bob Hanlon Feb 12 '17 at 13:44
  • $\begingroup$ Thank you! I changed one last time, could you give us an eye? I promise this is the last time in this thread. $\endgroup$ – TeM Feb 12 '17 at 15:30
4
$\begingroup$

The way you are thinking about the problem is hindering you -- you ask: how do I fill the entries of a matrix with certain values? This means you first create a matrix and then proceed to set its values. A more idiomatic way to envision this problem is: I have a list in a particular form, how do I change its form (select elements, partition them, reshape the array) to achieve the desired form? Proceeding this way, for your first case you want to partition the original list:

Partition[list, 2]

For the second, you want to select the elements of the list you want to keep, and then partition:

Partition[list[[All, 1]], 2]
{{a, d}, {g, j}, {m, p}}

And now for the third: here you apply (Map) the Grid function to the selected elements and then partition.

Partition[Map[Grid, list, {2}][[All, 1]], 2]
{{Grid[a], Grid[d]}, {Grid[g], Grid[j]}, {Grid[m], Grid[p]}}

Or, keeping all the options:

Partition[Map[Grid[#, Frame -> All, ItemSize -> All] &, list, {2}][[All, 1]], 2]

Frankly, these final versions do not make a lot of sense. Grid is a display command... filling a matrix with Grid elements is not going to give you a matrix that can be used for anything.

$\endgroup$
  • 1
    $\begingroup$ Manu -- you are doing it again. In this case, you do not want to make a matrix of elements, each of which is a Grid object. Grid is a display command... so you make the thing you want to display and then apply the display command(s) to the complete item. Nonetheless, I have shown above how to do this. $\endgroup$ – bill s Feb 12 '17 at 15:45
3
$\begingroup$
matrix = ArrayReshape[list, Dimensions[matrix]]

or

matrix[[All, All]] = ArrayReshape[list, Dimensions[matrix]]

assuming list is of the appropriate length.

$\endgroup$

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