Filling in Matrix (Interpolation
Mostrar comentarios más antiguos
If I have a (10,3) matrix like so,
10 20 1
0 0 0
0 0 0
40 80 4
0 0 0
60 120 6
0 0 0
0 0 0
0 0 0
80 200 10
Is there any way I can interpolate the data to get it to turn like so which displays the average values in between the endpoints? Using interp1 was not clear to me and resulted in NaN everywhere.
10 20 1
20 40 2
30 60 3
40 80 4
50 100 5
60 120 6
65 140 7
70 160 8
75 180 9
80 200 10
Any and all help/guidance is very much appreciated.
Respuesta aceptada
Más respuestas (2)
John BG
el 10 de Feb. de 2017
Hi Suki
A=[ 10 20 1
0 0 0
0 0 0
40 80 4
0 0 0
60 120 6
0 0 0
0 0 0
0 0 0
80 200 10]
B=A;
[sz1 sz2]=size(B);
for s=1:1:sz2
L=B(:,s);
[nx ny val]=find(L)
for k=2:1:numel(nx)
L([nx(k-1):1:nx(k)])=linspace(val(k-1),val(k),nx(k)-nx(k-1)+1);
end
B(:,s)=L;
end
A
=
10 20 1
0 0 0
0 0 0
40 80 4
0 0 0
60 120 6
0 0 0
0 0 0
0 0 0
80 200 10
B
=
10 20 1
20 40 2
30 60 3
40 80 4
50 100 5
60 120 6
65 140 7
70 160 8
75 180 9
80 200 10
Suki
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
D. Plotnick
el 10 de Feb. de 2017
Editada: D. Plotnick
el 10 de Feb. de 2017
You can either break up the columns, or do it in one go. Example below.
In future, please include how you ran interp1 or any other code, since that will make helping you easier.
% This returns the right answer
x = [1,4,6,10]; %indexes of known points
y1 = [1,4,6,10]; % y values at those points
Xq = 1:11; % add an extra point so you can see how extrap works.
Yq = interp1(x,y1,Xq,'linear','extrap');
% This also returns the right answer
y2 = [20,80,120,200];
Yq2 = interp1(x,y2,Xq,'linear','extrap');
% Also the right answer
y3 = [10 40 60 80];
Yq3 = interp1(x,y3,Xq,'linear','extrap');
% In one swell-foop
% We can do this in one go. The meshgrids are to make sure you have matrices of % the right size.
z = 1:3; % Index for columns.
Y = cat(1,y3,y2,y1);
[Xq,Zq] = meshgrid(Xq,z);
% Extrap does not work with interp2.
YqAll = interp2(x,z,Y,Xq,Zq,'linear')'
% you could also add an extrapval after the 'linear', which sets those
% NaNs to that number
2 comentarios
Stephen23
el 10 de Feb. de 2017
I guess that is one way to avoid the fowl/foul/fell confusion: http://www.quickanddirtytips.com/education/grammar/one-fell-swoop
D. Plotnick
el 11 de Feb. de 2017
Hah, I always assumed it was a Shakespeareanism of some kind but didn't know the specific source. I have to give credit for 'swell-foop' to Piers 'Xanth' Anthony.
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!