4
$\begingroup$

Probably very simple, but I don't know where to start with this. How do I go about creating a symbolic matrix, such as the one below?

enter image description here

I want to be able to use this so that any list of lists that are entered to signify a matrix can be read as having indices {i,1,m},{j,1,n}, and then I can use another rule using the terms in the matrix, ai,j.

I found an answer that mentioned SymbolicMatrix, but this no longer seems to work on the most up-to-date versions of Mathematica.

EDIT: This seems to be trickier than I first thought. Essentially, whenever I enter a list of lists, such as mat[{{1,2,3},{4,5,6},{7,8,9}], I want to be able to automatically recognise that the first list corresponds to a1,1,a1,2,a1,3, the second list to a2,1,a2,2,a2,3, and the third list (in this example) to a3,1,a3,2,a3,3.

I then aim to write a rule that, when values of I and j are selected, gives back a list of {ai,j+1, ai,j-1, ai-1,j, ai+1,j}.

$\endgroup$
5
  • $\begingroup$ Table[Subscript[a, i, j], {i, 3}, {j, 4}] // MatrixForm $\endgroup$ Commented Jan 5, 2021 at 17:22
  • $\begingroup$ Thanks, this helps a lot. Is there anyway that I can set the number of terms to be indefinite? I want the rule that I will write for the von Neumann neighborhood to work for any size of rectangular array. I tried with replacing '3' and '4' by 'm' and 'n' respectively, but get an error message that the iterator does not contain appropriate bounds. $\endgroup$ Commented Jan 5, 2021 at 17:24
  • 2
    $\begingroup$ Or try Array[a, {4, 5}]. $\endgroup$
    – bill s
    Commented Jan 5, 2021 at 17:25
  • 1
    $\begingroup$ I would also second @bills suggestion to stay away from Subscript expressions and use indexed variables instead, as was shown with Array. Much cleaner to handle those a[1, 2] objects later on! $\endgroup$
    – MarcoB
    Commented Jan 5, 2021 at 17:37
  • $\begingroup$ Or Array[Subscript[a, #1, #2] &, {4, 5}] $\endgroup$
    – chris
    Commented Jan 5, 2021 at 17:39

2 Answers 2

3
$\begingroup$
myMatrix[n_Integer, m_Integer] := 
 Table[Subscript[a, i, j], {i, n}, {j, m}] // MatrixForm

myMatrix[3, 4] 
$\endgroup$
2
  • $\begingroup$ Changing Subscript[a, n, m] to Subscript[a, i, j] works. Thanks! $\endgroup$ Commented Jan 5, 2021 at 17:34
  • $\begingroup$ If you want a symmetric matrix: myMatrix[n_Integer, m_Integer] := Table[Subscript[a, Sequence @@ Sort[{i, j}]], {i, n}, {j, m}] // MatrixForm $\endgroup$
    – chris
    Commented Jan 5, 2021 at 17:38
2
$\begingroup$

As mentioned in the comments, it is probably easier to stay away from using subscripts, which are primarily a formatting command. Your task is readily accomplished using Array, and defining a function to extract the elements you want:

mat = Array[a, {4, 5}]
fun[m_, i_, j_] := {a[i, j + 1], a[i, j - 1], a[i - 1, j], a[i + 1, j]};

mat defines the matrix and fun extracts the specified elements. For example:

fun[mat, 3, 4]
{a[3, 5], a[3, 3], a[2, 4], a[4, 4]}
$\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.