# Zero padding in Matlab

I have an image and I want to make a selection. First of all, I need to connect zeros between pixels so that [1,2,3] is converted to [1,0,2,0,3]. Can anyone tell me how to do this without using `paddarray`

or using loops `for`

?

Thank you in advance!

+3

source to share

4 answers

Something like that?:

``````B=zeros(size(img)*2);
B(1:2:end,1:2:end)=img;
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

However, there are ways to increase sampling in Matlab without having to do it manually, for example `interp2`

+5

source

You can also use MATLAB's way of dynamically allocating variables if you don't specify a number for the index in the array. By disabling indexing to specific locations in your array, MATLAB fills those values ββwith zeros by default. Thus:

``````B(1:2:5) = 1:3

B =

1   0   2   0   3
```

(adsbygoogle = window.adsbygoogle || []).push({});

```
+4

source

``````V = [1,2,3];

padded(numel(V)*2) = 0;
padded(1:2:end) = V
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

And then just handle the trailing zero if it `numel(V)`

was odd

+3

source

There is a function `upsample`

that does exactly that from Octave-Forge - see the docs at upsample .

Or you can look at the source`upsample`

to see what implements it. Are you against using a package or feature?

+1

source

All Articles