Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to construct a Matrix using the Table[] command. The issue is that I would like to use more than one expression for it. What I need to obtain is something like this :

{{f[1, 1], g[1, 1], f[1, 2], g[1, 2]}, {f[2, 1], g[2, 1], f[2, 2],g[2, 2]}}

I tried to write a table containing the two expressions f[i,j] and g[i,j] inside the Table[] command but without success... What I obtain instead is something like this :

In[8]:= Table[{f[i, j], g[i, j]}, {i, 2}, {j, 2}]

Out[8]= {{{f[1, 1], g[1, 1]}, {f[1, 2], g[1, 2]}}, {{f[2, 1], 
   g[2, 1]}, {f[2, 2], g[2, 2]}}}

What would be the correct syntax to construct these type of matrices? Or am I completely wrong trying to use the Table[] command?

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

Perhaps there is a more direct way, but you may do for example:

Join @@@ Table[{f[i, j], g[i, j]}, {i, 2}, {j, 2}]

or

Table[Sequence @@ {f[i, j], g[i, j]}, {i, 2}, {j, 2}]

and of course also

Array[Sequence @@ {f[##], g[##]} &, {2, 2}]
share|improve this answer
    
OK! Thanks a lot! I think I prefer the second option though e.g. Table[Sequence @@ {f[i, j], g[i, j]}, {i, 2}, {j, 2}] ... I don't know why but it seems more natural to me. –  jrojasqu Jan 26 '13 at 14:20
add comment
 ArrayReshape[Table[{f[i, j], g[i, j]}, {i, 2}, {j, 2}], {4, 2}]

or

 ArrayReshape[Array[{f[##], g[##]} &, {2, 2}], {4, 2}]
share|improve this answer
    
Thanks again for your help! I didn't know these ArrayReshape commands, they're going to help out a lot for other matters too! –  jrojasqu Jan 26 '13 at 14:21
add comment

I believe the heart of your question is about using Sequence within Table or similar constructs. You can use Join or Flatten after the fact, or you can write Sequence in a way that does not evaluate at the wrong point.

One naively would try:

Table[Sequence[f[i, j], g[i, j]], {i, 2}, {j, 2}]

But this results in an invalid syntax:

Table[f[i, j], g[i, j], {i, 2}, {j, 2}]

This is because Table does not have the attribute SequenceHold and therefore the Sequence is inserted before the Table attempts to evaluate. On the other hand ## & behaves just like Sequence except that it is held by regular HoldFirst/HoldAll attribute functions. (As ## &[] I call it the vanishing function.)

Therefore you need:

Table[## &[f[i, j], g[i, j]], {i, 2}, {j, 2}]
{{f[1, 1], g[1, 1], f[1, 2], g[1, 2]}, {f[2, 1], g[2, 1], f[2, 2], g[2, 2]}}

Or, written in maximally terse fashion and using Array:

Array[## &[f@##, g@##] &, {2, 2}]
{{f[1, 1], g[1, 1], f[1, 2], g[1, 2]}, {f[2, 1], g[2, 1], f[2, 2], g[2, 2]}}
share|improve this answer
    
Wow, ok I get the idea, thanks for the information! Although I'm still wondering, where can one find information on all these symbols like ##, @@, /;, etc? Because really I'm an enthusiast and I do believe that the "Documentation Center" explains things pretty well, but not when it comes to all of these symbols... which behave themselves like functions... Anyways thanks a lot! –  jrojasqu Jan 26 '13 at 18:40
    
@jrojasqu You can enter these directly in the Doc Center search box, or highlight one of them and press F1. For example ## will bring up the page on SlotSequence of which it is a short form. Also, try reading through the Virtual Book (under the Help menu). For example: Core Language > Functional Operations > Pure Functions is this tutorial. Also, don't be afraid to ask in Chat. –  Mr.Wizard Jan 26 '13 at 18:48
    
Hi, I tried entering ## on the "Documentation Center" and of course found the page on SlotSequence... But I didn't know about this Virtual Book or the tutorial! Thanks for the hint! –  jrojasqu Jan 27 '13 at 15:31
    
I may have another question regarding your answer : what do you mean by "maximally terse"? Does it mean that it is just the most concise way in terms of logic? Or does it mean that it is the lowest time-consumming way of constructing these types of matrices? –  jrojasqu Jan 27 '13 at 15:47
    
@jrojasqu I meant shortest code by character count (within reason -- I didn't strip white space or anything silly like that). I am not saying that it is the highest performance way, though in Mathematica short code often is faster than long code by nature of using built-in functions as opposed to manual constructs. If performance is paramount a different approach is likely better. Is it? –  Mr.Wizard Jan 27 '13 at 18:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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