0
votes

So I'm currently working on filling a 2-D matrix in order to calibrate a picture I've taken.

To do so I've created:

vector1 = linspace(0.037, 0.03175, 256);

This vector is ideally the first row in the matrix.

I then have another vector:

vector2 = linspace(0.0288, 0.0277, 256);

I was wondering if there is any way to interpolate between vector1 and vector2 and have them be inserted into a matrix with Row 1 = vector1 and Row 168 = vector2.

I should have 168 total rows, with 256 columns.

Any help would be greatly appreciated.

I'd rather not have to do 168 rows manually....

3

3 Answers

3
votes

If you want simple linear interpolation between the two, you can apply a weight to each of the vectors V1 and V2 and add them together. At the top, V1 will be weighted by 1 and V2 should be weighted by 0. Similarly at the bottom, V2 should be weighted by 1 and V1 should be weighted by 0. Every where else the weights should add to 1 and will weight V1 and V2 based upon how close the row is to the top or bottom.

V3 = alpha * V1 + (1 - alpha) * V2;

We can use matrix multiplication to create the weighted versions of V1 and V2 and then add the result together to get V3.

nRows = 168;

alpha = linspace(1, 0, nRows);
V3 = alpha(:) * V1 + (1 - alpha(:)) * V2;
3
votes

you can just use interp1 as follows:

m = interp1([1 2],[vector1;vector2],linspace(1,2,168))
1
votes

While for your specific application it is probably overkill, but you can use griddata to perform 2d bilinear interpolation using your input points. This would not necessarily give the same result as a manual 1d interpolation of your data, although in this specific case I believe the result will probably be the same:

H = 168;
W = 256;
vector1 = linspace(0.037,0.03175,W);
vector2 = linspace(0.0288,0.0277,W);
[Wmat,Hmat] = meshgrid(1:W,1:H);
img = griddata([1:W,1:W],[ones(1,W), H*ones(1,W)],[vector1 vector2],Wmat,Hmat,'linear')

The result seems to be correct:

>> all(abs(img(1,:)-vector1)<1e-10)

ans =

     1

>> all(abs(img(end,:)-vector2)<1e-10)

ans =

     1