# Defining a matrix with a recurrent sequence

How can a matrix A[n] of size $2^n \times 2^n$ be defined given that

A= {{1, -1}, {1, 1}}


and for $n \ge 1$,

A[n + 1] = {{A[n], -A[n]}, {A[n], A[n]}}


I tried to use SparseArray and assigned the values, but it's limited to set of values I can assign, which doesn't work. Also, I thought to try this : setting a size s= 2^n and then using it in a array x = Array[n, {s, s}], but I get stuck on how to set the elements to 1 and -1.

Can you please guide me in right direction or tell me if I'm going in the wrong direction?

• Look up ArrayFlatten – Szabolcs Oct 12 '17 at 12:14
• But how can the sequence be defined? Have to do something with 2^n-1 – Ahmed Oct 12 '17 at 12:17

A = {{1, -1}, {1, 1}}
A[n_] := A[n] = ArrayFlatten[{{A[n - 1], -A[n - 1]}, {A[n - 1], A[n - 1]}}]
(* {{1, -1}, {1, 1}} *)

A // MatrixPlot References:

KroneckerProduct[] is the more natural function to use for generating Walsh-like matrices:

mat = {{1, -1}, {1, 1}};
amat[n_Integer?NonNegative] := Nest[KroneckerProduct[mat, #] &, mat, n - 1]

MatrixPlot[amat] 