D. Sukhov
D. Sukhov

Reputation: 361

Resize matrix in Julia

How do I convert a matrix A

   A = [a b c 
        d e f 
        g h i]

To matrix B?

   B = [a a b b c c
        a a b b c c
        d d e e f f
        d d e e f f
        g g h h i i
        g g h h i i]

Upvotes: 3

Views: 121

Answers (2)

sethaxen
sethaxen

Reputation: 71

It's slightly slower than repeat, but for a numeric array, you can also use kron:

julia> A = permutedims(reshape(1:9, 3, 3))
3×3 Matrix{Int64}:
 1  2  3
 4  5  6
 7  8  9

julia> kron(A, ones(Int, 2, 2))
6×6 Matrix{Int64}:
 1  1  2  2  3  3
 1  1  2  2  3  3
 4  4  5  5  6  6
 4  4  5  5  6  6
 7  7  8  8  9  9
 7  7  8  8  9  9

Upvotes: 1

Nils Gudat
Nils Gudat

Reputation: 13800

You can use repeat:

julia> A = ['a' 'b' 'c'
            'd' 'e' 'f'
            'g' 'h' 'i']
3×3 Matrix{Char}:
 'a'  'b'  'c'
 'd'  'e'  'f'
 'g'  'h'  'i'

julia> repeat(A, inner = (2, 2))
6×6 Matrix{Char}:
 'a'  'a'  'b'  'b'  'c'  'c'
 'a'  'a'  'b'  'b'  'c'  'c'
 'd'  'd'  'e'  'e'  'f'  'f'
 'd'  'd'  'e'  'e'  'f'  'f'
 'g'  'g'  'h'  'h'  'i'  'i'
 'g'  'g'  'h'  'h'  'i'  'i'

Upvotes: 6

Related Questions