雖然您確實需要提供一個真實的代碼示例和引發錯誤的輸入數據的實際版本,但我會對此做一個嘗試。代碼和數據,你投遞工作就好了:
f =
2.95 3.325 3.9 3.95
你得到的錯誤意味着實際x
向量不排序,因爲它是在你的例子這裏,否則它包含重複的值(即x = [0 1 1 2 3];
) 。您可以使用issorted(x)
命令測試第一種情況,使用any(diff(x) == 0)
測試第二種情況。
第一種情況很容易解決:
[x,ix] = sort(x);
y = y(ix);
xq = sort(xq);
yq = interp1(x,y,xq);
有幾個方法來處理第二種情況,根據您的應用程序。您可以搜索重複項並將它們移動±eps
,將它們平均或丟棄。
雖然沒有重複您的錯誤的數據,但我們都只是猜測。
更新:
看您發佈的數據(和對子孫後代的緣故,你應該直接編輯成你的問題),你有兩個問題。您的數據沒有排序,並有重複。正如我所說,排序很簡單。 排序如上之後,這是我在過去所做的那樣採取重複的條目的護理:
xu = unique(x);
yu = y;
duplicated = xu(histc(x,xu) > 1); % find the duplicated entries
discardIndex = [];
for k = 1:length(duplicated);
dupIndex = sort(find(x == duplicated(k))); % look for duplicated entries
keepIndex = dupIndex(1); % keep only the first one
discardIndex = [discardIndex dupIndex(2:end)]; % add the rest to a list
yu(keepIndex) = mean(y(:,dupIndex)); % take the mean of the y values at the duplicated x values
end
yu(discardIndex) = []; % after all is said and done, delete the duplicated entries.
我寫了很長一段時間以前,幾乎可以肯定是不這樣做的最有效方法,但它會起作用。
我注意到你的'x'和'y'數組的大小不一樣。 – eigenchris 2014-12-03 18:57:27
對不起,我組成了一些代表我的數據的數組,但沒有實際的副本,我編輯了數組以反映您的評論,並確保它們的大小相同。對於我的數據,我可以向你保證x和y的大小相同。 – DirectNirvana 2014-12-03 19:01:51
也許'resample'可以提供幫助。 – Rashid 2014-12-03 19:04:35