1
$\begingroup$

I would like to create an $m \times n$-table A with entries Ai_j, where $1 \leq i \leq m$ and $1 \leq j \leq n$. I have seen similar questions with subscripts, but I need the entries to be exactly of the form Ai_j, since I'm importing data from MatLab.

To be more specific, I have a list of equations with variables Ai_j from Matlab and I want to solve them with Mathematica. For this I need to specify for which variables to solve, but I'm to lazy to type them in one-for-one (its a huge matrix) so I just wanted to use Flatten[A].

As written in the comments, another option would be to transport the matrix A=sym('A',[m,n]) from Matlab to Mathematica, but I don't know how either.

$\endgroup$
10
  • 2
    $\begingroup$ What do you mean by "I need the entries to be exactly of the form Ai_j, since I'm importing data from MatLab"? Please be more specific. BTW, since you mentioned MATLAB, are you aware of this issue?: mathematica.stackexchange.com/q/10582/1871 $\endgroup$
    – xzczd
    Commented Mar 27, 2020 at 9:23
  • $\begingroup$ I have a list of equations with variables Ai_j from Matlab and I want to solve them with Mathematica. For this I need to specify for which variables to solve, but I'm to lazy to type them in one-for-one (its a huge matrix) so I just wanted to use Flatten[A] $\endgroup$ Commented Mar 27, 2020 at 9:23
  • $\begingroup$ …Well, if I understand correctly, you don't know Part ([[]]) can be used on multi-dimensional lists, too? If so, an example: mat={{1,2},{4,5}}; mat[[2, 1]]. Please check the document of Part for more info. $\endgroup$
    – xzczd
    Commented Mar 27, 2020 at 9:27
  • $\begingroup$ No, I give you an example. Given an equation 4*A3_5 +8*A7_9==0, I would like to solve it for {A3_5,A7_9}. Now, I have a large number of much more complicated equations in variables Ai_j. So, how to effectively specify the list of variables in Solve? $\endgroup$ Commented Mar 27, 2020 at 9:34
  • 2
    $\begingroup$ Do you mean you're importing symbolic expressions like Ai_j from MATLAB? If so, a more severe problem is, you cannot use _ for variable naming in Mathematica, because it's the short form of built-in function Pattern. $\endgroup$
    – xzczd
    Commented Mar 27, 2020 at 9:55

1 Answer 1

2
$\begingroup$

Does the below code suffice?

{m, n} = {3, 5};
Array[Subscript[Symbol["A" <> ToString[#]], #2] &, {m, n}]

Update

Now that it has been figured out how to generate symbols without underscores in MATLAB, I provide a Wolfram version here:

Array[Symbol[StringTemplate["A````"][##]] &, {m, n}]
$\endgroup$
7
  • $\begingroup$ Unfortunately not, because of the subscript, but as xzczd pointed out in the comments of the OP, what I wanted doesn't work $\endgroup$ Commented Mar 27, 2020 at 10:07
  • $\begingroup$ @BipolarMinds Do you mean to let underscores (_) appear in the symbols? $\endgroup$ Commented Mar 27, 2020 at 10:10
  • $\begingroup$ yes, appareantly this is not allowed for variables $\endgroup$ Commented Mar 27, 2020 at 10:13
  • 2
    $\begingroup$ @BipolarMinds OK, it looks like you have learned about it. So I suggest you import those variables as Strings and then delete (e.g., using StringDelete) the underscore characters before converting them into Symbols, so that you will have "Ai_j" $ \rightarrow $ "Aij" $ \rightarrow $ Aij. $\endgroup$ Commented Mar 27, 2020 at 10:16
  • 2
    $\begingroup$ @BipolarMinds Ah, it turns out to be A = sym('A%d%d', [3, 5]) in MATLAB. $\endgroup$ Commented Mar 27, 2020 at 10:44

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.