Effacer les filtres
Effacer les filtres

interpolation vector with a lot of duplicate values code on R2010a

5 vues (au cours des 30 derniers jours)
judy abbott
judy abbott le 21 Sep 2017
Modifié(e) : Jan le 26 Sep 2017
Please how i can use this code on Matlab R2010
y1 = [350 770 800 920 970 990 1020 1054 1080 1100];
x1=[10 10 11 14 13 12 10 10 10 7];
x2 = [10 10 13 13 15 13 13 10 10 10];
[C,ia,idx] = unique(x1,'stable');
val = accumarray(idx,y1,[],@mean); %You could take something other than the mean.
your_vals = interp1(C,val,x2,'linear','extrap'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
plot(x1,y1,'b*',x2,your_vals,'r+');
i get
??? Error using ==> unique
Unrecognized option

Réponse acceptée

Jan
Jan le 22 Sep 2017
Modifié(e) : Jan le 26 Sep 2017
You can emulate the modern unique('stable') by:
function [AA, AI, BI] = UniqueStable(A)
% Equivalent to: unique(A(:), 'stable')
% Inptus: single, double (U)INT8/16/31/64, char
% Author: Jan Simon, Heidelberg 2017, License: CC BY-SA 3.0
nA = numel(A);
if nA == 0
AA = [];
AI = [];
BI = [];
else
[As, SV] = sort(A(:));
if nargout == 1
UV(SV) = ([true; diff(As) ~= 0]);
AA = A(UV);
else % Indices requested:
UV = ([true; diff(As) ~= 0]);
UVs(SV) = UV;
AI = find(UVs).';
AA = A(UVs);
% Complex creation of BI such that AA(BI) == A:
v = zeros(nA, 1);
v(AI) = 1:length(AI); % Sequence related to AA
vs = v(SV); % Sorted like A
vf = vs(vs ~= 0); % Just the filled entries
BI = zeros(nA, 1); % Pre-allocate
BI(SV) = vf(cumsum(UV)); % Inflate multiple elements
end
end
[EDITED] Note: This is even faster than Matlab's unqiue(x, 'stable'):
x = rand(1, 1e3);
f1 = @() UniqueStable(x);
f2 = @() unique(x, 'stable');
timeit(f1, 3)
timeit(f2, 3)
% 0.000191363010829893
% 0.000382956117288057
For 1e6 elements:
% 0.358884703612193
% 0.490294384698773

Plus de réponses (1)

Walter Roberson
Walter Roberson le 21 Sep 2017
The 'stable' option was introduced some time after R2010*

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by