0
$\begingroup$

I would like to translate the following code into Mathematica language but am having some trouble doing so:

h1=1;
L1=Array[f,9];
for(i=1,i<4,i++)
 {
   for(j=1,j<4,j++)
    {
     q1=Part[SomeMatrix,i,j]
     q2=Apart[q1]
     f[h1]=q2
     h1++
    }
 }

the aim is to perform 2 operations on each cell of a matrix and then create a new 3X3 matrix out of the predefined array L1.

help will be much appreciated. Thank you in advance

$\endgroup$
1
  • 3
    $\begingroup$ As a start, try to forget the looping approach completely. Describe what you would like to accomplish (what operation, on what matrix, with what result). Typically you can carry that out with Map, or Table. $\endgroup$
    – MarcoB
    Commented Feb 22, 2021 at 22:04

2 Answers 2

1
$\begingroup$

Is this what you're trying to accomplish?

Map[Apart, SomeMatrix, {2}]
$\endgroup$
1
$\begingroup$
(* create an example matrix *)
mA = Array[(Subscript[c, #] + Subscript[d, #])/(Subscript[a, #] + 
      Subscript[b, #]) &, {3, 3}]

(* direct translation *)
h1 = 1;
L1 = Array[f, 9]
For[i = 1, i < 4, i++,
 For[j = 1, j < 4, j++, q1 = Part[mA, i, j]; q2 = Apart[q1]; 
  f[h1] = q2; h1++]]
L1

(* better approach *)
Apart /@ Catenate@mA
$\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.