Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

tls.m 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function [out] = tls(X, varargin)
  2. % s = tls(X,['ax'])
  3. % Fit a total-least-squares line to the data in X
  4. % X should have two columns. Each row is a pair of data points
  5. %
  6. % 'ax' is an axes handle. If it is passed in draw a line on the axis.
  7. %
  8. % s is a structure with the following fields:
  9. % s.slope slope of the line
  10. % s.int y-intercept of the line
  11. % s.linehandle handle to the line drawn on the axis (if 'ax' is passed in, otherwise [])
  12. if nargin==0
  13. %DEMO MODE
  14. X = randn(100,1)*10 + 35;
  15. X(:,2) = X(:,1) + randn(100,1)*5 - 20;
  16. figure()
  17. ax = draw.jaxes;
  18. plot(ax,X(:,1),X(:,2),'o')
  19. out = stats.tls(X,'ax',ax);
  20. out.eig1
  21. return
  22. end
  23. %%
  24. args = varargin;
  25. [ax, args] = utils.inputordefault('ax',[],args);
  26. utils.inputordefault(args);
  27. meanX = mean(X,1);
  28. nX = X - meanX;
  29. [V,~] = eig(nX'*nX);
  30. out.eig1 = V(:,1);
  31. out.slope = -(out.eig1(1))/(out.eig1(2));
  32. out.intercept = meanX(2) - out.slope*meanX(1);
  33. if ~isempty(ax)
  34. a = get(ax,'xlim');
  35. b = out.slope*a + out.intercept;
  36. out.linehandle = plot(ax,a,b,'r');
  37. else
  38. out.linehandle = [];
  39. end
  40. end