Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with 2d or 3d data... (plane fit/lsplane.m)

Subject: Help with 2d or 3d data... (plane fit/lsplane.m)

From: Allen

Date: 21 Jun, 2005 01:46:42

Message: 1 of 5

Hi everyone,

I'm extremely new to matlab, so I'm very sorry if I'm missing some
simple things... I've been trying to learn as much as possible... I need
some help thinking about this current problem.

I have height data taken from an AFM (atomic force microscope), each z
height (nanometers) is taken at some x,y position (also in nanometers).
  Currently I have imported the z array (256x256 double), and have also
created an x and y array(s) corresponding to the positions along x and y
of each point. The data can be specified in 3d, in that it has a
position in x, y, and z. I would like to plane-fit the data with say,
the lsplane.m file of I.M. Smith (included below for those who don't
have it from the repository). [or other plane-fitting routine you may
suggest]

I have some confusion about my data and the array required for
lsplane.m. Here's the input array information (quoting): "Array [x y
z] where x=vector of x-coords, y=vector of y-coords, z=vector of z
coords, Dimension mx3."

Now, how are my X, Y, Z matrices related to the array Lsplane.m is
requesting? I can't seem to merge the data together into a 3d array.
[in a spread sheet, the x,y would simply be the index for the z data...
x's size of course is 1x256 (so is Y, but I can transpose it), and Z's
size is 256x256.

Any help appreciated in dealing with my silly confusion... as I said,
I'm painfully new to all of this. :) Always fun. I guess I'll be
learning a lot. ;)

Thanks very much for any help and suggestions. I greatly appreciate it!!
-Allen

ps- please feel free to e-mail me regarding the question, but I will
assume I need to look here so that the answer will be helpful to all who
come after me. ;) I did do a lot of looking in the google archives and
help files etc., first before asking, but perhaps not enough... [for
e-mail demunge the address]

---------function: lsplane.m included from repository-------------

   function [x0, a, d, normd] = lsplane(X)
% ---------------------------------------------------------------------
% LSPLANE.M Least-squares plane (orthogonal distance
% regression).
%
% Version 1.0
% Last amended I M Smith 27 May 2002.
% Created I M Smith 08 Mar 2002
% ---------------------------------------------------------------------
% Input
% X Array [x y z] where x = vector of x-coordinates,
% y = vector of y-coordinates and z = vector of
% z-coordinates.
% Dimension: m x 3.
%
% Output
% x0 Centroid of the data = point on the best-fit plane.
% Dimension: 3 x 1.
%
% a Direction cosines of the normal to the best-fit
% plane.
% Dimension: 3 x 1.
%
% <Optional...
% d Residuals.
% Dimension: m x 1.
%
% normd Norm of residual errors.
% Dimension: 1 x 1.
% ...>
%
% [x0, a <, d, normd >] = lsplane(X)
% ---------------------------------------------------------------------
% check number of data points
   m = size(X, 1);
   if m < 3
     error('At least 3 data points required: ' )
   end
%
% calculate centroid
   x0 = mean(X)';
%
% form matrix A of translated points
   A = [(X(:, 1) - x0(1)) (X(:, 2) - x0(2)) (X(:, 3) - x0(3))];
%
% calculate the SVD of A
   [U, S, V] = svd(A, 0);
%
% find the smallest singular value in S and extract from V the
% corresponding right singular vector
   [s, i] = min(diag(S));
   a = V(:, i);
%
% calculate residual distances, if required
   if nargout > 2
     d = U(:, i)*s;
     normd = norm(d);
   end
% ---------------------------------------------------------------------
% End of LSPLANE.M.

Subject: Help with 2d or 3d data... (plane fit/lsplane.m)

From: heiko_marx@hotmail.com

Date: 21 Jun, 2005 07:35:58

Message: 2 of 5

Allen schrieb:
> Hi everyone,
>
> I'm extremely new to matlab, so I'm very sorry if I'm missing some
> simple things... I've been trying to learn as much as possible... I need
> some help thinking about this current problem.
>
> I have height data taken from an AFM (atomic force microscope), each z
> height (nanometers) is taken at some x,y position (also in nanometers).
> Currently I have imported the z array (256x256 double), and have also
> created an x and y array(s) corresponding to the positions along x and y
> of each point. The data can be specified in 3d, in that it has a
> position in x, y, and z. I would like to plane-fit the data with say,
> the lsplane.m file of I.M. Smith (included below for those who don't
> have it from the repository). [or other plane-fitting routine you may
> suggest]
>
> I have some confusion about my data and the array required for
> lsplane.m. Here's the input array information (quoting): "Array [x y
> z] where x=vector of x-coords, y=vector of y-coords, z=vector of z
> coords, Dimension mx3."
>
> Now, how are my X, Y, Z matrices related to the array Lsplane.m is
> requesting? I can't seem to merge the data together into a 3d array.
> [in a spread sheet, the x,y would simply be the index for the z data...
> x's size of course is 1x256 (so is Y, but I can transpose it), and Z's
> size is 256x256.

Hi.

I assume the routine takes only triples of x,y,z data (actual space
coordinates). So if your z data is 2d, just generate appropriate x and
y array that are the same size as z. Look for meshgrid in the help.
Then you have as much x and y indices as you need for your z data.
Meshgrid simply repeats the x and y values as often as needed for the z
data. When reshaping x,y, and z to 256*256 x 1 vectors the routine
might work.

Hope I understood your problem,
Heiko

Subject: Help with 2d or 3d data... (plane fit/lsplane.m)

From: Allen

Date: 21 Jun, 2005 14:17:49

Message: 3 of 5


heiko_marx@hotmail.com wrote:
> Allen schrieb:
>>I have height data taken from an AFM (atomic force microscope), each z
>>height (nanometers) is taken at some x,y position (also in nanometers).
>> Currently I have imported the z array (256x256 double), and have also
>>created an x and y array(s) corresponding to the positions along x and y
>>of each point. The data can be specified in 3d, in that it has a
>>position in x, y, and z. I would like to plane-fit the data with say,
>>the lsplane.m file of I.M. Smith (included below for those who don't
>>have it from the repository). [or other plane-fitting routine you may
>>suggest]
>>
>>I have some confusion about my data and the array required for
>>lsplane.m. Here's the input array information (quoting): "Array [x y
>>z] where x=vector of x-coords, y=vector of y-coords, z=vector of z
>>coords, Dimension mx3."
>>
>>Now, how are my X, Y, Z matrices related to the array Lsplane.m is
>>requesting? I can't seem to merge the data together into a 3d array.
>>[in a spread sheet, the x,y would simply be the index for the z data...
>>x's size of course is 1x256 (so is Y, but I can transpose it), and Z's
>>size is 256x256.
>
>
> Hi.
>
> I assume the routine takes only triples of x,y,z data (actual space
> coordinates). So if your z data is 2d, just generate appropriate x and
> y array that are the same size as z. Look for meshgrid in the help.
> Then you have as much x and y indices as you need for your z data.
> Meshgrid simply repeats the x and y values as often as needed for the z
> data. When reshaping x,y, and z to 256*256 x 1 vectors the routine
> might work.
>
> Hope I understood your problem,
> Heiko
>

Heiko,

Thank you very much for your suggestion. I will expand my matrices so
that one is completely x filled 256x256, one y filled, and one z
(original data)... then each position x1, y1, z1 will be a point in
space. Of course the x and y data will have redundant info, but that's
the only way I can imagine it being filled out.

I'll let you know of my success or failure. :)

Cheers!
-Allen

Subject: Help with 2d or 3d data... (plane fit/lsplane.m)

From: Nick Cui

Date: 19 Apr, 2011 02:33:05

Message: 4 of 5

Thanks I also found this useful.

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

Subject: Help with 2d or 3d data... (plane fit/lsplane.m)

From: Roger Stafford

Date: 19 Apr, 2011 04:05:24

Message: 5 of 5

Allen <ajhalldeleteme@gmaildot.com> wrote in message <d98d4i$7ao$1@news.ks.uiuc.edu>...
> Hi everyone,
>
> I'm extremely new to matlab, so I'm very sorry if I'm missing some
> simple things... I've been trying to learn as much as possible... I need
> some help thinking about this current problem.
>
> I have height data taken from an AFM (atomic force microscope), each z
> height (nanometers) is taken at some x,y position (also in nanometers).
> Currently I have imported the z array (256x256 double), and have also
> created an x and y array(s) corresponding to the positions along x and y
> of each point. The data can be specified in 3d, in that it has a
> position in x, y, and z. I would like to plane-fit the data with say,
> the lsplane.m file of I.M. Smith (included below for those who don't
> have it from the repository). [or other plane-fitting routine you may
> suggest]
>
> I have some confusion about my data and the array required for
> lsplane.m. Here's the input array information (quoting): "Array [x y
> z] where x=vector of x-coords, y=vector of y-coords, z=vector of z
> coords, Dimension mx3."
>
> Now, how are my X, Y, Z matrices related to the array Lsplane.m is
> requesting? I can't seem to merge the data together into a 3d array.
> [in a spread sheet, the x,y would simply be the index for the z data...
> x's size of course is 1x256 (so is Y, but I can transpose it), and Z's
> size is 256x256.
> ........
- - - - - - - - -
  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.

  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.

  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.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us