Index exceeds the number of array elements error and interp1

3 views (last 30 days)
Hi all,
I'm trying to write a script that takes spectra from a spectrometer with x-axis being wavenumbers taken over a range and y-axis being intensity and interpolates the data to resample the data and align it with new wavenumbers from a different spectrometer on the x axis
I'm using the interp1 function but i'm not sure it's working right. This is what I have so far:
x = table2array(KLWavenumber); %KLWavenumber (or x) is a 1045x1 double now
v = table2array(CollagenLow); %CollagenLow (or v) is a 1045x1 double now
xq = table2array(MyLower); %MyLower (or xq) is a 1045x1 double now
[x, index] = unique(x);
yi = interp1(x, y(index), xi);
figure
vq1 = interp1(x,v,xq);
plot(x,v,'o',xq,vq1,':.');
title('(Default) Linear Interpolation');
So KLWavenumber and CollagenLow is the x and y axis from the old spectrometer which has been manually imported by me into the workspace, giving a table which I've changed to double (for another script). MyLower is the new wavenumber x axis that I want the data to resample to i.e the query points identified for finer sampling over the range of x(KLWavenumber).
Initially it threw out an "Error using griddedInterpolant: The grid vectors must contain unique points." error, however, after a quick search on this forum, this is supposedly fixed by adding in
[x, index] = unique(x);
yi = interp1(x, y(index), xi);
Although, now I'm getting the dreaded "Index exceeds the number of array elements (952)." error and I'm not sure how to get rid of it.
Looking at the workspace, index is a 1015x1 double. Could someone explain to me where it's going wrong and also what determines the index size?
Thanks!
  1 Comment
Walter Roberson
Walter Roberson on 16 Aug 2019
You do not happen to show creating y or its size, so we as outside observers must guess that y is not the same number of elements as x has. Perhaps you wanted to interpolate v or xq ?

Sign in to comment.

Answers (1)

Urmila Rajpurohith
Urmila Rajpurohith on 19 Aug 2019
[x, index] = unique(x);
When you use “unique” function as above, the "index" stores the indices of the unique elements that are present in “x”.
Index is a 1051x1 vector means there are 1051 unique elements present in “x” and index holds the indices of those 1051 unique elements.
Assuming “y” is declared in the program.
From the error it is clear that “y” is an array with 952 elements and when you are trying to access the elements from 953 to 1015(As 1015 is the size of “index”) it throws error saying “Index exceeds the number of array elements (952)”.
If you need to interpolate “y” then try to match the sizes of “y” and “index”
Else if you need to interpolate between “x” and “v” try
yi = interp1(x, v(index), xi);
As “v” contains CollagenLow data.

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!