0

I am attempting to pad an an array with zeros but cannot find a succinct way to do this in swift. If I have array x = [1,2,3 ; 4,5,6 ; 7,8,9] in matlab I could use the command

y = [zeros(1,3+2);zeros(3,1),x,zeros(3,1);zeros(1,3+2)]

giving the desired output array of [0,0,0,0,0; 0,1,2,3,0 ; 0,4,5,6,0 ; 0,7,8,9,0 ; 0,0,0,0,0]

However so far in a swift playground I have only been able to refer to each element individually to correctly form the new array.

The methods I have tried so far using x as the input and y as the output, the first similar to matlab,

var x = [[1,2,3,],[4,5,6],[7,8,9]]

var y = [[0,0,0,0,0],[0,x[0],0],[0,x[1],0],[0,x[2],0],[0,0,0,0,0]]

The second being a loop

for i in 0 ..< x.count + 1 {
  if i == 0 || i == x.count - 1 {
    y[i] = [0,0,0,0,0]
  }
  else{
    y[i] = [0, x[i-1] ,0]
  }
}

Rather than looking like a standard array in the Xcode playground preview this is the output given.

[[0, 0, 0, 0, 0], [0, […], 0], [0, 0, 0, 0, 0], [0, […], 0], [0, 0, 0, 0, 0]]

which also prints to the console very strangely, using the code

for i in 0 ..< y.count {
    print("\(y[i])")
}

the output is

(
    0,
    0,
    0,
    0,
    0
)
(
    0,
        (
        1,
        2,
        3
    ),
    0
)
(
    0,
    0,
    0,
    0,
    0
)
(
    0,
        (
        7,
        8,
        9
    ),
    0
)
(
    0,
    0,
    0,
    0,
    0
)

as opposed to the expected

[0, 0, 0, 0, 0]
[0, 1, 2, 3, 0]
[0, 4, 5, 6, 0]
[0, 7, 8, 9, 0]
[0, 0, 0, 0, 0]

What is the best way to do this?

Hamish
  • 78,605
  • 19
  • 187
  • 280
Andrew
  • 85
  • 2
  • 8

3 Answers3

2

I made a generic version of appzYourLife's answer that can take any arbitrary nested array type. It also adds support for top and bottom padding

extension Array where Element: _ArrayType {
    typealias InnerElement = Element.Generator.Element

    func pad2DArray(with padding: InnerElement,
                    top: Int = 0, left: Int = 0,
                    right: Int = 0, bottom: Int = 0) -> [[InnerElement]] {
        let newHeight = self.count + top + bottom
        let newWidth = (self.first?.count ?? 0) + left + right

        var paddedArray = [[InnerElement]](count: newHeight, repeatedValue:
                        [InnerElement](count: newWidth, repeatedValue: padding))

        for (rowIndex, row) in self.enumerate() {
            for (columnIndex, element) in row.enumerate() {
                paddedArray[rowIndex + top][columnIndex + left] = element
            }
        }

        return paddedArray
    }
}

var input = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

let result = input.pad2DArray(with: 0, top: 1, left: 1, right: 1, bottom: 1)

/*
result:
[
    [0, 0, 0, 0, 0],
    [0, 1, 2, 3, 0],
    [0, 4, 5, 6, 0],
    [0, 7, 8, 9, 0],
    [0, 0, 0, 0, 0],
]
*/
Community
  • 1
  • 1
Alexander
  • 59,041
  • 12
  • 98
  • 151
-1

Extension

If you define this extension

extension _ArrayType where Element == Int {
    func pad(left left: Int, right: Int) -> [Int] {
        let leftSide = [Int](count: left, repeatedValue: 0)
        let rightSide = [Int](count: right, repeatedValue: 0)
        return leftSide + (self as! [Int]) + rightSide
    }
}

you can then write

[1,2,3].pad(left: 1, right: 1) // [0, 1, 2, 3, 0]
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
-1

Definitely not as elegant as the accepted answer, but it works nonetheless:

var x = [[1,2,3,],[4,5,6],[7,8,9]]
var y = [[Int]]()

y.insert([0,0,0,0,0], atIndex: 0)

for i in 0 ..< x.count {
    var intArray: [Int] = []
    for number in x[i] {
        intArray.append(number)
    }
    intArray.insert(0, atIndex: 0)
    intArray.insert(0, atIndex: intArray.count)
    y.append(intArray)
}

y.insert([0,0,0,0,0], atIndex: x.count + 1)

The output from

for i in 0 ..< y.count {
    print("\(y[i])")
}

is

[0, 0, 0, 0, 0]

[0, 1, 2, 3, 0]

[0, 4, 5, 6, 0]

[0, 7, 8, 9, 0]

[0, 0, 0, 0, 0]

Community
  • 1
  • 1
Shades
  • 5,568
  • 7
  • 30
  • 48