## what does this line of code means bwZ = [zeros(1,c​+1);[zeros​(r,1) bw]]; ??

### Shahd Ewawi (view profile)

on 2 Apr 2013
```bw=imread('connected.pgm')./255;
%bw=[0   1   0   0   1   1;
%    1   1   1   0   0   0;
%    0   0   1   0   0   1;
%    1   1   0   0   1   1;
%    0   0   0   1   0   0;];
[r,c] = size(bw);
```
```bwZ = [zeros(1,c+1);[zeros(r,1) bw]];
```

i dont understand this line of code :

```bwZ = [zeros(1,c+1);[zeros(r,1) bw]];
```

## Products

No products are associated with this question.

### Walter Roberson (view profile)

on 2 Apr 2013

The line adds a row of zeros along the top, and a column of zeros along the side, of bw.

Another way of expressing it would have been:

```bwZ = zeros(r+1, c+1, class(bw));
bwZ(2:end, 2:end) = bw;
```

Shahd Ewawi

### Shahd Ewawi (view profile)

on 3 Apr 2013

this code is for connected components i dont understand why do we have to add a row of zeros along the top, and a column of zeros along the side.

Walter Roberson

### Walter Roberson (view profile)

on 3 Apr 2013

It can make the search algorithm easier to write.

For example, if the task were to find the beginning of each "pulse" in a vector of 0 and 1 values, then one way to do that is to search for places in which you have 0 (non-pulse) followed by 1 (pulse). But that search algorithm would fail if the first item in the vector was a 1, as there is no 0 before it. A solution to that is to put a 0 before the vector and then you would not need special code to handle the situation.

Shahd Ewawi

### Shahd Ewawi (view profile)

on 5 Apr 2013

Thank you ^^

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi