Suppose I have a matrix of size(3,3).This is up-scaled by 2 making it (6,6).So there would be a number of vacant values between each element of the initial (3,3) matrix.Then how do you fill up the 0 values in addmatrixR by bicubic/liner interpolation

1 view (last 30 days)
prevR=magic(3)
m=1;
k1=1
addRmatrix= zeros(2*size(prevR,1),2*size(prevR,2))
for i=1:size(prevR,1);
for j=1:size(prevR,2);
addRmatrix(m,k1)= prevR(i,j);
k1=k1+2 ;
end
m=m+2;
k1=1;
end

Accepted Answer

Stephen23
Stephen23 on 17 Jul 2017
Edited: Stephen23 on 17 Jul 2017
Just use interp2:
>> M = magic(3)
M =
8 1 6
3 5 7
4 9 2
>> interp2(M,1)
ans =
8.0000 4.5000 1.0000 3.5000 6.0000
5.5000 4.2500 3.0000 4.7500 6.5000
3.0000 4.0000 5.0000 6.0000 7.0000
3.5000 5.2500 7.0000 5.7500 4.5000
4.0000 6.5000 9.0000 5.5000 2.0000
Or the simplest of all:
>> interp2(M)
ans =
8.0000 4.5000 1.0000 3.5000 6.0000
5.5000 4.2500 3.0000 4.7500 6.5000
3.0000 4.0000 5.0000 6.0000 7.0000
3.5000 5.2500 7.0000 5.7500 4.5000
4.0000 6.5000 9.0000 5.5000 2.0000
If you need to specify the sample points, then do something like this:
>> [Xi,Yi] = meshgrid(1:1.0:S(2),1:1.0:S(1));
>> [Xo,Yo] = meshgrid(1:0.5:S(2),1:0.5:S(1));
>> out = interp2(Xi,Yi, M, Xo,Yo)
out =
8.0000 4.5000 1.0000 3.5000 6.0000
5.5000 4.2500 3.0000 4.7500 6.5000
3.0000 4.0000 5.0000 6.0000 7.0000
3.5000 5.2500 7.0000 5.7500 4.5000
4.0000 6.5000 9.0000 5.5000 2.0000
  4 Comments
MSP
MSP on 17 Jul 2017
Edited: MSP on 17 Jul 2017
Thanks a lot,But could you elaborate the third code for me. What does Xo and Yo specify
Stephen23
Stephen23 on 17 Jul 2017
Edited: Stephen23 on 17 Jul 2017
@MSP: Xi and Yi are sample locations of the input data values: I defined these arbitrarily to be with steps of one. Xo and Yo are the sample locations of the output data values: I defined these to have half the step size of the input steps (steps of 0.5).

Sign in to comment.

More Answers (1)

C.J. Harris
C.J. Harris on 17 Jul 2017
Just use interp2 on the original matrix, like so:
prevR = magic(3)
ans =
8 1 6
3 5 7
4 9 2
addRmatrix = interp2(prevR,1)
ans =
8.0000 4.5000 1.0000 3.5000 6.0000
5.5000 4.2500 3.0000 4.7500 6.5000
3.0000 4.0000 5.0000 6.0000 7.0000
3.5000 5.2500 7.0000 5.7500 4.5000
4.0000 6.5000 9.0000 5.5000 2.0000
  1 Comment
MSP
MSP on 17 Jul 2017
But for practical cases the addRmatrix say isnt exactly double dimension,Then what to do.How do we have addRmatrix of dimension 7*8.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!