1

So I was looking at this question:

Matlab - Standard Deviation of Cartesian Points

Which basically answers my question, except the problem is I have xyz, not xy. So I don't think Ax=b would work in this case.


I have, say, 10 Cartesian points, and I want to be able to find the standard deviation of these points. Now, I don't want standard deviation of each X, Y and Z (as a result of 3 sets) but I just want to get one number.

This can be done using MATLAB or excel.


To better understand what I'm doing, I have this desired point (1,2,3) and I recorded (1.1,2.1,2.9), (1.2,1.9,3.1) and so on. I wanted to be able to find the variability of all the recorded points.

I'm open for any other suggestions.

Community
  • 1
  • 1
Smiley
  • 75
  • 1
  • 11
  • So do you just want the RMS distance from all recorded points to the desired point, or are you interested in findin a 3D line or plane to which you can compute the distance? – chappjc Apr 15 '14 at 07:33
  • You just brought up a good point, finding the RMS distance of all recorded to desired point is one way of finding the variability. Unfortunately, I just realized that I do not have an exact desired point, so I'm guessing a 3D line to compute the distance would be better-- that is to say that this distance would be the error -- can I call that variability as well? – Smiley Apr 15 '14 at 18:21
  • I'll post a solution with a best fit 3D line shortly. Call it error or variability, as long as you know what it indicates. BTW, extending the answer from the other question directly as David did fits a _plane_, not a line, although that is an option. – chappjc Apr 15 '14 at 18:26
  • Or you can just compute the distances to the centroid of the points, but if you expect them to be along a line, then I'd fit a line. – chappjc Apr 15 '14 at 19:18
  • @chappjc -- I don't expect them to be along a line, so computing distance to centroid of the points make more sense. Everyday I learn something new! – Smiley Apr 15 '14 at 23:18
  • OK, let me update my answer so I can maybe get something from this. But at least the answers are good references for the related jobs! :) – chappjc Apr 15 '14 at 23:23
  • OK, updated. I hope it's useful. Perhaps the question can be edited so that a search for something like "fitting/distance to 3D line and/or plane" will point to the question. That way David's and my answers might be useful in the future. – chappjc Apr 15 '14 at 23:32
  • @chappjc you're very helpful! I changed the question so hope that's better. I wish I can upvote both answers, but I don't have enough reputation. When I do, I will! – Smiley Apr 16 '14 at 20:11

2 Answers2

2

If you do the same thing as in the other answer you linked, it should work.

x_vals = xyz(:,1);
y_vals = xyz(:,2);
z_vals = xyz(:,3);

then make A with 3 columns,

A = [x_vals y_vals ones(size(x_vals))];

and

b = z_vals;

Then

sol=A\b;
m = sol(1);
n = sol(2);
c = sol(3);

and then

errs = (m*x_vals + n*y_vals + c) - z_vals;

After that you can use errs just as in the linked question.

David
  • 8,449
  • 1
  • 22
  • 32
  • Can xyz coordinates work for Ax=b? I really need to review my linear algebra, but I'm trying to understand what you did. Why would you choose A like that? What's the method you used here? Thanks – Smiley Apr 15 '14 at 18:26
  • @Smiley I believe he meant to have `b = z_vals;`. This solution will fit a plane to the data, I believe. – chappjc Apr 15 '14 at 18:27
  • @Smiley Yep chappjc is correct, I should have have `b=z_vals;`. You can fit many types of equations this way, not just 1D linear ones, e.g.: http://en.wikipedia.org/wiki/Vandermonde_matrix#Applications – David Apr 15 '14 at 22:17
  • Thank you so much! Your answer and @chappjc 's answer are very helpful! I might be using both. – Smiley Apr 16 '14 at 20:13
2

Randomly clustered data

If your data is not expected to be near a line or a plane, just compute the distance of each point to the centroid:

xyz_bar = mean(xyz);
M = bsxfun(@minus,xyz,xyz_bar);
d = sqrt(sum(M.^2,2)); % distances to centroid

Then you can compute variability anyway you like. For example, standard deviation and RMS error:

std(d)
sqrt(mean(d.^2))

Data about a 3D line

If the data points are expected to be roughly along the path of a line, with some deviation from it, you might look at the distance to a best fit line. First, fit a 3D line to your points. One way is using the following parametric form of a 3D line:

x = a*t + x0
y = b*t + y0
z = c*t + z0

Generate some test data, with noise:

abc = [2 3 1]; xyz0 = [6 12 3];
t = 0:0.1:10;
xyz = bsxfun(@plus,bsxfun(@times,abc,t.'),xyz0) + 0.5*randn(numel(t),3)
plot3(xyz(:,1),xyz(:,2),xyz(:,3),'*') % to visualize

Estimate the 3D line parameters:

xyz_bar = mean(xyz) % centroid is on the line
M = bsxfun(@minus,xyz,xyz_bar); % remove mean
[~,S,V] = svd(M,0)
abc_est = V(:,1).'
abc/norm(abc) % compare actual slope coefficients

Distance from points to a 3D line:

pointCentroidSeg = bsxfun(@minus,xyz_bar,xyz);
pointCross = cross(pointCentroidSeg, repmat(abc_est,size(xyz,1),1));
errs = sqrt(sum(pointCross.^2,2))

Now you have the distance from each point to the fit line ("error" of each point). You can compute the mean, RMS, standard deviation, etc.:

>> std(errs)
ans =
    0.3232
>> sqrt(mean(errs.^2))
ans =
    0.7017

Data about a 3D plane

See David's answer.

Community
  • 1
  • 1
chappjc
  • 30,359
  • 6
  • 75
  • 132