How to fix this error in interp1?

Hi,
I am trying to interpolate X values from Y values using a set of imported data.
The problem is that when I set both x and y as unique values, they do not have the same length, and I get the following error message:
Error using interp1>reshapeAndSortXandV (line 424)
X and V must be of the same length.
Error in interp1 (line 93)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2});
Error in FindMax (line 18)
x2 = interp1(y3, x3, y2, 'linear');
The code:
clear; clc;
y=load ('X.txt');
x=load ('Y.txt');
plot(x(:,2),-y(:,2));
y1=-y(:,2);
x1=x(:,2);
y3=unique(y1);
x3=unique(x1);
a= size(y3)
b= size(x3)
y2 = [100.0 10.0 95.0];
x2 = interp1(y3, x3, y2, 'linear');
figure(1)
plot(x1, y1, '-g')
hold on
plot(x2, y2, 'bp')
hold off
grid
legend('Data', 'Interpolated Points', 'Location', 'NW')

Réponses (1)

Walter Roberson
Walter Roberson le 4 Avr 2019
You cannot fix that. Your x has duplicate values, as it goes left and then back towards the right again. The number of unique values in y is not going to match the number of unique values in x.
perhaps:
[y3, yidx] = sort(y3);
x3 = x1(yidx);
x2 = interp2(y3, x3, y2, 'linear');
However, the maximum y3 value is -1.17e-9 . You cannot justify linear interpolation to the y2=10 or y2=100 range.

4 commentaires

Ismail Qeshta
Ismail Qeshta le 8 Avr 2019
Modifié(e) : Ismail Qeshta le 8 Avr 2019
Hi Walter,
Thank you for your comment. That is actually sad to hear. I have been using the below code for interpolating my outputs. I think you helped me with it a while ago. It used to work on my previous sets of data, but now it says that they should be unique.
Is it possible if we can use other type of interpolation than the linear one just to get it work?
I actually need to run the interpolation on my current set data, so I am trying to find a way to do it. I attached herewith a sample data to show that the code does not work for them.
The code:
clear; clc;
Folder = cd;
N=100;
x2 = zeros(N, 10);
for k = 1:N;
Driftt = sprintf('X%d.out', k);
Reactt = sprintf('Y%d.out', k);
matDrift = importdata(fullfile(Folder, Driftt));
matReact = importdata(fullfile(Folder, Reactt));
x1= matDrift(:,2);
y1= -matReact(:,2);
[x3, ix] = unique(x1);
y3 = y1(ix);
A=dlmread('AllResults.txt');
for i=1:size(A,2)
x2 (k,:) = interp1(y3, x3, A(:,i), 'linear');
temp=x2(k,:);
temp(isnan(temp))=0.05;
x2(k,:)=temp;
fid=fopen(['result_' num2str(i) '.txt'],'a');
fprintf(fid,'%f\n',x2(k,:));
fclose(fid);
end
end
Walter Roberson
Walter Roberson le 8 Avr 2019
What relationship do those two .txt files have to that code?
The Y1.txt file has only two columns, but the above code expects 11 columns.
We do not have AllResults.txt either ?
Ismail Qeshta
Ismail Qeshta le 8 Avr 2019
Hi Walter,
Sorry, I just corrected the above code, it is x1= matDrift(:,2);
y1= -matReact(:,2);
I have also attached the missing file in my comment above.
Walter Roberson
Walter Roberson le 8 Avr 2019
In my code, I sorted y3 and I took the sorted y3 as the first parameter to interp1()
In your code, you unique x1 producing x3, and you take x3 as the second parameter to interp1()
You have to be doing the sort or unique with respect to the parameter that you are going to use first in interp1()
Your x1 are already unique, and it is your y1 that are not unique, but you are treating y1 as your independent variable and x1 as your dependent variable.
Consider for example y1 = 3008570 . When asked to interpolate at that value, do you want to return x1 = 16 (array index 74), or do you want to return x1 just slightly different than 544.75 (near array index 2189, which is y1 = 3008750)? If asked to interpolate at y1 = 3008750 then should the result be just slightly different than x1 = 16 (array index 74), or should it be x1 = 544.75 (array index 2189) ? That is, should the choice of whether to interpolate based upon the rise or the descent be based upon which happens to be the closer match to the interpolating location, rather than trying to pick consistently from the rise or the descent ?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interpolation dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by