Filling in Matrix (Interpolation

43 Ansichten (letzte 30 Tage)
Suki Sandhu
Suki Sandhu am 10 Feb. 2017
Kommentiert: Stephen23 am 11 Feb. 2017
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 10 Feb. 2017
Bearbeitet: Stephen23 am 10 Feb. 2017
Here is one easy way to use interp1, it just takes two lines of code:
>> M = [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];
>> X = ~all(M==0,2);
>> N = interp1(find(X),M(X,:),1:size(M,1))
N =
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
  2 Kommentare
D. Plotnick
D. Plotnick am 11 Feb. 2017
Bearbeitet: D. Plotnick am 11 Feb. 2017
Very nice, hadn't thought about using the logical indexing. Also, I hadn't realized interp1 could handle multiple rows like that. Has that always been true? And, more importantly, does that work on pages as well? i.e., interp1 will always assume it is working along the first dimension of any N-D array?
Scratch that, I just tried it and it works. Amazing!
Try
M = repmat(M,1,1,5);
X = all(~all(M==0,2),3);
N = interp1(find(X),M(X,:,:),1:size(M,1))
Stephen23
Stephen23 am 11 Feb. 2017
It works along columns, exactly as the documentation says: "...you can pass v as an array. Each column of array v contains a different set of 1-D sample values".

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

John BG
John BG am 10 Feb. 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
D. Plotnick am 10 Feb. 2017
Bearbeitet: D. Plotnick am 10 Feb. 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 Kommentare
Stephen23
Stephen23 am 10 Feb. 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
D. Plotnick am 11 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by