http://www.mathworks.com/matlabcentral/newsreader/view_thread/98419
MATLAB Central Newsreader  Help with 2d or 3d data... (plane fit/lsplane.m)
Feed for thread: Help with 2d or 3d data... (plane fit/lsplane.m)
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 21 Jun 2005 01:46:42 +0000
Help with 2d or 3d data... (plane fit/lsplane.m)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/98419#249655
Allen
Hi everyone,<br>
<br>
I'm extremely new to matlab, so I'm very sorry if I'm missing some <br>
simple things... I've been trying to learn as much as possible... I need <br>
some help thinking about this current problem.<br>
<br>
I have height data taken from an AFM (atomic force microscope), each z <br>
height (nanometers) is taken at some x,y position (also in nanometers). <br>
Currently I have imported the z array (256x256 double), and have also <br>
created an x and y array(s) corresponding to the positions along x and y <br>
of each point. The data can be specified in 3d, in that it has a <br>
position in x, y, and z. I would like to planefit the data with say, <br>
the lsplane.m file of I.M. Smith (included below for those who don't <br>
have it from the repository). [or other planefitting routine you may <br>
suggest]<br>
<br>
I have some confusion about my data and the array required for <br>
lsplane.m. Here's the input array information (quoting): "Array [x y <br>
z] where x=vector of xcoords, y=vector of ycoords, z=vector of z <br>
coords, Dimension mx3."<br>
<br>
Now, how are my X, Y, Z matrices related to the array Lsplane.m is <br>
requesting? I can't seem to merge the data together into a 3d array. <br>
[in a spread sheet, the x,y would simply be the index for the z data... <br>
x's size of course is 1x256 (so is Y, but I can transpose it), and Z's <br>
size is 256x256.<br>
<br>
Any help appreciated in dealing with my silly confusion... as I said, <br>
I'm painfully new to all of this. :) Always fun. I guess I'll be <br>
learning a lot. ;)<br>
<br>
Thanks very much for any help and suggestions. I greatly appreciate it!!<br>
Allen<br>
<br>
ps please feel free to email me regarding the question, but I will <br>
assume I need to look here so that the answer will be helpful to all who <br>
come after me. ;) I did do a lot of looking in the google archives and <br>
help files etc., first before asking, but perhaps not enough... [for <br>
email demunge the address]<br>
<br>
function: lsplane.m included from repository<br>
<br>
function [x0, a, d, normd] = lsplane(X)<br>
% <br>
% LSPLANE.M Leastsquares plane (orthogonal distance<br>
% regression).<br>
%<br>
% Version 1.0<br>
% Last amended I M Smith 27 May 2002.<br>
% Created I M Smith 08 Mar 2002<br>
% <br>
% Input<br>
% X Array [x y z] where x = vector of xcoordinates,<br>
% y = vector of ycoordinates and z = vector of<br>
% zcoordinates.<br>
% Dimension: m x 3.<br>
%<br>
% Output<br>
% x0 Centroid of the data = point on the bestfit plane.<br>
% Dimension: 3 x 1.<br>
%<br>
% a Direction cosines of the normal to the bestfit<br>
% plane.<br>
% Dimension: 3 x 1.<br>
%<br>
% <Optional...<br>
% d Residuals.<br>
% Dimension: m x 1.<br>
%<br>
% normd Norm of residual errors.<br>
% Dimension: 1 x 1.<br>
% ...><br>
%<br>
% [x0, a <, d, normd >] = lsplane(X)<br>
% <br>
% check number of data points<br>
m = size(X, 1);<br>
if m < 3<br>
error('At least 3 data points required: ' )<br>
end<br>
%<br>
% calculate centroid<br>
x0 = mean(X)';<br>
%<br>
% form matrix A of translated points<br>
A = [(X(:, 1)  x0(1)) (X(:, 2)  x0(2)) (X(:, 3)  x0(3))];<br>
%<br>
% calculate the SVD of A<br>
[U, S, V] = svd(A, 0);<br>
%<br>
% find the smallest singular value in S and extract from V the<br>
% corresponding right singular vector<br>
[s, i] = min(diag(S));<br>
a = V(:, i);<br>
%<br>
% calculate residual distances, if required<br>
if nargout > 2<br>
d = U(:, i)*s;<br>
normd = norm(d);<br>
end<br>
% <br>
% End of LSPLANE.M.

Tue, 21 Jun 2005 07:35:58 +0000
Re: Help with 2d or 3d data... (plane fit/lsplane.m)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/98419#249726
heiko_marx@hotmail.com
Allen schrieb:<br>
> Hi everyone,<br>
><br>
> I'm extremely new to matlab, so I'm very sorry if I'm missing some<br>
> simple things... I've been trying to learn as much as possible... I need<br>
> some help thinking about this current problem.<br>
><br>
> I have height data taken from an AFM (atomic force microscope), each z<br>
> height (nanometers) is taken at some x,y position (also in nanometers).<br>
> Currently I have imported the z array (256x256 double), and have also<br>
> created an x and y array(s) corresponding to the positions along x and y<br>
> of each point. The data can be specified in 3d, in that it has a<br>
> position in x, y, and z. I would like to planefit the data with say,<br>
> the lsplane.m file of I.M. Smith (included below for those who don't<br>
> have it from the repository). [or other planefitting routine you may<br>
> suggest]<br>
><br>
> I have some confusion about my data and the array required for<br>
> lsplane.m. Here's the input array information (quoting): "Array [x y<br>
> z] where x=vector of xcoords, y=vector of ycoords, z=vector of z<br>
> coords, Dimension mx3."<br>
><br>
> Now, how are my X, Y, Z matrices related to the array Lsplane.m is<br>
> requesting? I can't seem to merge the data together into a 3d array.<br>
> [in a spread sheet, the x,y would simply be the index for the z data...<br>
> x's size of course is 1x256 (so is Y, but I can transpose it), and Z's<br>
> size is 256x256.<br>
<br>
Hi.<br>
<br>
I assume the routine takes only triples of x,y,z data (actual space<br>
coordinates). So if your z data is 2d, just generate appropriate x and<br>
y array that are the same size as z. Look for meshgrid in the help.<br>
Then you have as much x and y indices as you need for your z data.<br>
Meshgrid simply repeats the x and y values as often as needed for the z<br>
data. When reshaping x,y, and z to 256*256 x 1 vectors the routine<br>
might work.<br>
<br>
Hope I understood your problem,<br>
Heiko

Tue, 21 Jun 2005 14:17:49 +0000
Re: Help with 2d or 3d data... (plane fit/lsplane.m)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/98419#249795
Allen
<br>
heiko_marx@hotmail.com wrote:<br>
> Allen schrieb:<br>
>>I have height data taken from an AFM (atomic force microscope), each z<br>
>>height (nanometers) is taken at some x,y position (also in nanometers).<br>
>> Currently I have imported the z array (256x256 double), and have also<br>
>>created an x and y array(s) corresponding to the positions along x and y<br>
>>of each point. The data can be specified in 3d, in that it has a<br>
>>position in x, y, and z. I would like to planefit the data with say,<br>
>>the lsplane.m file of I.M. Smith (included below for those who don't<br>
>>have it from the repository). [or other planefitting routine you may<br>
>>suggest]<br>
>><br>
>>I have some confusion about my data and the array required for<br>
>>lsplane.m. Here's the input array information (quoting): "Array [x y<br>
>>z] where x=vector of xcoords, y=vector of ycoords, z=vector of z<br>
>>coords, Dimension mx3."<br>
>><br>
>>Now, how are my X, Y, Z matrices related to the array Lsplane.m is<br>
>>requesting? I can't seem to merge the data together into a 3d array.<br>
>>[in a spread sheet, the x,y would simply be the index for the z data...<br>
>>x's size of course is 1x256 (so is Y, but I can transpose it), and Z's<br>
>>size is 256x256.<br>
> <br>
> <br>
> Hi.<br>
> <br>
> I assume the routine takes only triples of x,y,z data (actual space<br>
> coordinates). So if your z data is 2d, just generate appropriate x and<br>
> y array that are the same size as z. Look for meshgrid in the help.<br>
> Then you have as much x and y indices as you need for your z data.<br>
> Meshgrid simply repeats the x and y values as often as needed for the z<br>
> data. When reshaping x,y, and z to 256*256 x 1 vectors the routine<br>
> might work.<br>
> <br>
> Hope I understood your problem,<br>
> Heiko<br>
> <br>
<br>
Heiko,<br>
<br>
Thank you very much for your suggestion. I will expand my matrices so <br>
that one is completely x filled 256x256, one y filled, and one z <br>
(original data)... then each position x1, y1, z1 will be a point in <br>
space. Of course the x and y data will have redundant info, but that's <br>
the only way I can imagine it being filled out.<br>
<br>
I'll let you know of my success or failure. :)<br>
<br>
Cheers!<br>
Allen

Tue, 19 Apr 2011 02:33:05 +0000
Re: Help with 2d or 3d data... (plane fit/lsplane.m)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/98419#831856
Nick Cui
Thanks I also found this useful.<br>
<br>
Allen <ajhalldeleteme@gmaildot.com> wrote in message <d98d4i$7ao$1@news.ks.uiuc.edu>...<br>
> Hi everyone,<br>
> <br>
> I'm extremely new to matlab, so I'm very sorry if I'm missing some <br>
> simple things... I've been trying to learn as much as possible... I need <br>
> some help thinking about this current problem.<br>
> <br>
> I have height data taken from an AFM (atomic force microscope), each z <br>
> height (nanometers) is taken at some x,y position (also in nanometers). <br>
> Currently I have imported the z array (256x256 double), and have also <br>
> created an x and y array(s) corresponding to the positions along x and y <br>
> of each point. The data can be specified in 3d, in that it has a <br>
> position in x, y, and z. I would like to planefit the data with say, <br>
> the lsplane.m file of I.M. Smith (included below for those who don't <br>
> have it from the repository). [or other planefitting routine you may <br>
> suggest]<br>
> <br>
> I have some confusion about my data and the array required for <br>
> lsplane.m. Here's the input array information (quoting): "Array [x y <br>
> z] where x=vector of xcoords, y=vector of ycoords, z=vector of z <br>
> coords, Dimension mx3."<br>
> <br>
> Now, how are my X, Y, Z matrices related to the array Lsplane.m is <br>
> requesting? I can't seem to merge the data together into a 3d array. <br>
> [in a spread sheet, the x,y would simply be the index for the z data... <br>
> x's size of course is 1x256 (so is Y, but I can transpose it), and Z's <br>
> size is 256x256.<br>
> <br>
> Any help appreciated in dealing with my silly confusion... as I said, <br>
> I'm painfully new to all of this. :) Always fun. I guess I'll be <br>
> learning a lot. ;)<br>
> <br>
> Thanks very much for any help and suggestions. I greatly appreciate it!!<br>
> Allen<br>
> <br>
> ps please feel free to email me regarding the question, but I will <br>
> assume I need to look here so that the answer will be helpful to all who <br>
> come after me. ;) I did do a lot of looking in the google archives and <br>
> help files etc., first before asking, but perhaps not enough... [for <br>
> email demunge the address]<br>
> <br>
> function: lsplane.m included from repository<br>
> <br>
> function [x0, a, d, normd] = lsplane(X)<br>
> % <br>
> % LSPLANE.M Leastsquares plane (orthogonal distance<br>
> % regression).<br>
> %<br>
> % Version 1.0<br>
> % Last amended I M Smith 27 May 2002.<br>
> % Created I M Smith 08 Mar 2002<br>
> % <br>
> % Input<br>
> % X Array [x y z] where x = vector of xcoordinates,<br>
> % y = vector of ycoordinates and z = vector of<br>
> % zcoordinates.<br>
> % Dimension: m x 3.<br>
> %<br>
> % Output<br>
> % x0 Centroid of the data = point on the bestfit plane.<br>
> % Dimension: 3 x 1.<br>
> %<br>
> % a Direction cosines of the normal to the bestfit<br>
> % plane.<br>
> % Dimension: 3 x 1.<br>
> %<br>
> % <Optional...<br>
> % d Residuals.<br>
> % Dimension: m x 1.<br>
> %<br>
> % normd Norm of residual errors.<br>
> % Dimension: 1 x 1.<br>
> % ...><br>
> %<br>
> % [x0, a <, d, normd >] = lsplane(X)<br>
> % <br>
> % check number of data points<br>
> m = size(X, 1);<br>
> if m < 3<br>
> error('At least 3 data points required: ' )<br>
> end<br>
> %<br>
> % calculate centroid<br>
> x0 = mean(X)';<br>
> %<br>
> % form matrix A of translated points<br>
> A = [(X(:, 1)  x0(1)) (X(:, 2)  x0(2)) (X(:, 3)  x0(3))];<br>
> %<br>
> % calculate the SVD of A<br>
> [U, S, V] = svd(A, 0);<br>
> %<br>
> % find the smallest singular value in S and extract from V the<br>
> % corresponding right singular vector<br>
> [s, i] = min(diag(S));<br>
> a = V(:, i);<br>
> %<br>
> % calculate residual distances, if required<br>
> if nargout > 2<br>
> d = U(:, i)*s;<br>
> normd = norm(d);<br>
> end<br>
> % <br>
> % End of LSPLANE.M.

Tue, 19 Apr 2011 04:05:24 +0000
Re: Help with 2d or 3d data... (plane fit/lsplane.m)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/98419#831865
Roger Stafford
Allen <ajhalldeleteme@gmaildot.com> wrote in message <d98d4i$7ao$1@news.ks.uiuc.edu>...<br>
> Hi everyone,<br>
> <br>
> I'm extremely new to matlab, so I'm very sorry if I'm missing some <br>
> simple things... I've been trying to learn as much as possible... I need <br>
> some help thinking about this current problem.<br>
> <br>
> I have height data taken from an AFM (atomic force microscope), each z <br>
> height (nanometers) is taken at some x,y position (also in nanometers). <br>
> Currently I have imported the z array (256x256 double), and have also <br>
> created an x and y array(s) corresponding to the positions along x and y <br>
> of each point. The data can be specified in 3d, in that it has a <br>
> position in x, y, and z. I would like to planefit the data with say, <br>
> the lsplane.m file of I.M. Smith (included below for those who don't <br>
> have it from the repository). [or other planefitting routine you may <br>
> suggest]<br>
> <br>
> I have some confusion about my data and the array required for <br>
> lsplane.m. Here's the input array information (quoting): "Array [x y <br>
> z] where x=vector of xcoords, y=vector of ycoords, z=vector of z <br>
> coords, Dimension mx3."<br>
> <br>
> Now, how are my X, Y, Z matrices related to the array Lsplane.m is <br>
> requesting? I can't seem to merge the data together into a 3d array. <br>
> [in a spread sheet, the x,y would simply be the index for the z data... <br>
> x's size of course is 1x256 (so is Y, but I can transpose it), and Z's <br>
> size is 256x256.<br>
> ........<br>
        <br>
You should take note of the phrase "orthogonal distance regression" in the documentation of 'lsplane'. This means that the orthogonal mean square distances from your x,y,z points to the plane is to be minimized. This corresponds to a statistical situation where the expected errors are of equal magnitudes in all three dimensions.<br>
<br>
In a situation where you are not concerned with errors in x and y locations but only in z values, you would very likely prefer to minimize just the mean square differences between z values in the points and corresponding z values in the plane, and that is a different problem, namely ordinary linear regression with two independent variables.<br>
<br>
As heiko_marx states, you must use as input to 'lsplane' a single array which is 256^2 by 3 in which each row is a triplet of x, y, and z matching values.<br>
<br>
Roger Stafford