# Legacy SubMatrix function

I've started updating Mathematica code published in a 2000 paper:

Algorithms Behind Term Structure Models of Interest Rates II: The Hull-White Trinomial Tree of Interest Rates

The original code loads the legacy package LinearAlgebraMatrixManipulation from which it used the function:

SubMatrix[]


For a bit of context, the author uses the function in a Which statement within an Append[]:

Append[SubMatrix[XX, {2, 2}, {-jmin + jmax - 1, -jmin + jmax + 1}],
Delete[XX[[-1]], {{1}, {2}}]]


Can anyone describe what this function did in 2000 and then direct me to ways to do it in more recent versions of Mathematica (I currently use 10.3.0.0)?

• Search (e.g., with Google) for "mathematica LinearAlgebra MatrixManipulation" to obtain several references. Commented Sep 30, 2017 at 4:03
• Take[] or Part[] + Span[] should prove more than adequate to replace that old function. Commented Sep 30, 2017 at 5:34
• @J.M. -- Appears that Take[] has an exit replacement. -- Thx. Commented Sep 30, 2017 at 14:12
• @J.M. - I spoke to soon. See below. Commented Sep 30, 2017 at 15:12
• Yes, I didn't mean that it was a direct plug-in replacement; just that Take[] with properly set parameters can be used instead. Commented Sep 30, 2017 at 15:32

As suggested by @J.M. in the comments Take[] appears to provide an exact replacement for the legacy, SubMatrix[] (although one has to dig into the documentation to find it, listed as the 6th example under Take...Scope in version 10.3.0.0).

SubMatrix[XX, {2, 2}, {-jmin + jmax - 1, -jmin + jmax + 1}]

Take[XX, {2, 2}, {-jmin + jmax - 1, -jmin + jmax + 1}]


Well... Mr.Wizard called it properly in the 1st comment to this question. The above is indeed incorrect.

Let's try again...with Mr.Wizard's example:

m = Partition[Alphabet[], 4];
SubMatrix[m, {1, 1}, {2, 2}]

{{"a", "b"}, {"e", "f"}}


Now look at the following Take[]

Take[m, {1, 2}, {1, 2}]

{{"a", "b"}, {"e", "f"}}


I think that gets it (even it my answer got there in a roundabout way). Still not certain why the legacy SubMatrix worked differently than Take.

• FWIW a concise equivalent: Take[XX, {2}, {-1, 1} + jmax - jmin] -- however I believe that this answer is incorrect. Commented Sep 30, 2017 at 14:20
• @Mr.Wizard - As always, nice to have you on the case. Commented Sep 30, 2017 at 14:35

The legacy package is here: http://library.wolfram.com/infocenter/MathSource/6770/

TakeMatrix[mat_?MatrixQ, start:{startR_Integer, startC_Integer},
end:{endR_Integer, endC_Integer}] :=
Take[mat, {startR, endR}, {startC, endC}] /;
And @@ Thread[Dimensions[mat] >= start] &&

SubMatrix[mat_List, start:{_Integer, _Integer}, dim:{_Integer,_Integer}] :=
TakeMatrix[mat, start, start+dim-1]


This is not directly equivalent to Take:

m = Partition[Alphabet[], 4];

SubMatrix[m, {1, 1}, {2, 2}]
Take[m, {1, 1}, {2, 2}]

{{"a", "b"}, {"e", "f"}}

{{"b"}}


A terse but inefficient equivalent, omitting the argument tests:

subMat[mat_, start_, dim_] := Array[mat[[##]] &, dim, start]

• Take[m,{1,2},{1,2}] gives the same output as SubMatrix[m, {1, 1}, {2, 2}] in your example. Did that big a change happen in defining positions of Lists? Just trying to understand the thinking that took the legacy code to the current. Commented Sep 30, 2017 at 14:55
• @Jagra I think it is just two different ways of looking at the same thing. I don't see a big change, merely that SubMatrix wasn't apparently considered important enough to make a kernel function. Take` has existed since version 1.0 so it's not a matter of one function replacing the other, but rather about the need or lack thereof for a complementary function. Commented Sep 30, 2017 at 23:55