Andrew - 1 year ago 64

Swift Question

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?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**