3
$\begingroup$

I want to update "matrix1" 100 times. "matrix3" will be new "matrix1" and it will iterate 100 times. Should I use a loop or function? First iteration is:

matrix1 = ( {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
   } );
matrix2 = matrix1*2 - 1;
matrix3 = matrix2 + 5;
matrix3

Output (First iteration):

{{6, 8, 10}, {12, 14, 16}, {18, 20, 22}}

matrix3 will be new matrix1

matrix1 = ( {
    {6, 8, 10},
    {12, 14, 16},
    {18, 20, 22}
   } );
matrix2 = matrix1*2 - 1;
matrix3 = matrix2 + 5;
matrix3

Output (Second iteration):

{{16, 20, 24}, {28, 32, 36}, {40, 44, 48}}

And, It will repeat 100 times.

$\endgroup$
10
$\begingroup$
NestList[x \[Function] 2 x + 4, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 100]
$\endgroup$
7
$\begingroup$

Another possibility is to write the problem via a recursion:

f[n_] := 2 f[n - 1] + 4;
f[1] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

To get any power, you then ask for

f[3]
{{16, 20, 24}, {28, 32, 36}, {40, 44, 48}}

or f[100]

$\endgroup$
5
$\begingroup$

Just for illustration and learning reasons, three examples with Fold, Do and Table. Define

matrix1 := {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

Then

Table[matrix1 = 2*matrix1 + 4, {100}]

prints all intermediate matrices

Do[matrix1 = 2*matrix1 + 4, {100}]
matrix1

and

f[x_] := 2 x + 4
Fold[f[#1] &, matrix1, Range[100]]

print the last result.

$\endgroup$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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