views:

66

answers:

3

Hi. I have a large amount of data to import in to MATLAB, representing location of points in cartesian space. Which of the following is the most conventional for storing and processing standard XYZ data?:

OPTION #1

Store X, Y and Z coordinates as separate n * 1 vectors (possibly within a structure?). This makes:

  • Plotting simple: plot3(X, Y, Z)
  • Extracting individual points slightly more convoluted N = [X(i), Y(i), Z(i)]
  • Passing the entire set of points to a function expands the number of different arguments to pass.

OPTION #2

Store X, Y and Z coordinates as one n * 3 vector.

  • Plotting is slightly harder: plot3(XYZ(:, 1), XYZ(:, 2), XYZ(:, 3))
  • Extracting individual points is easier: N = XYZ(i, :)
  • Passing entire set of points is easy - just one variable

Based on this, I suspect the second is the more conventional.


However, some of the data I'll be processing is harder, as it is read in in grid form, where the format of the grid is important. By this, I mean that the raw form for the data is as a n * m * 3 matrix, instead of an (n*m) * 3 matrix. The fact that point X(i, j) is next to point X(i, j+1) is important. Again, two options are presented:

OPTION #1

Store each X, Y and Z coordinate as an n * m matrix.

OPTION #2

Store all the points as an n * m * 3 matrix.

I would rather do the second here, but some options, like plotting become quite silly:

X = XYZ(:, :, 1);
Y = XYZ(:, :, 2);
Z = XYZ(:, :, 3);
plot3(X(:), Y(:), Z(:));

I have a feeling that there must be a convention for this, particularly in the vision and graphics community.

A: 

In answer to the first half of your question, maintaining an n x 3 (or 3 x n) matrix is way easier, as it makes matrix maths syntax trivial, and more efficient. In general, you'll probably be doing a lot of maths operations and then a bit of display logic at the end, so I wouldn't worry about the one-off transformation you need to do to call e.g. plot3.

Regarding the second half of your question, I'm not sure what you mean by "grid", so I can't answer that!

Oli Charlesworth
Thanks, I thought as much. I've tried to clarify the second part of the question. (As an aside, which of n x 3 or 3 x n is more conventional?)
Bill Cheatham
+2  A: 

I don't think there is any one truly best way to do this. The best way for you is that which makes your life most convenient.

If you will do many 3-d plots and you prefer to carry around the data in one combined form, then write a little plot3d tool. Set it up to take an nx3 array (or a structure as you prefer) then it calls plot3, splitting the three columns.

% =============================
function h = plot3d(data,varargin)
% plots 3-d data (more help is good here)
h = plot3(data(:,1),data(:,2),data(:,3),varargin{:});
if nargout == 0
  clear h
end
% =============================

I'd add some extra error checks on the shape of data, and much better help, but you should get the general idea.

Personally, I like to carry 3-dimensional data around in one nx3 array. For other types of things, IMHO, a structure is best. For example, it makes sense to me to stuff both the vertices and the tessellation information from a delaunay triangulation into one structure.

Expand matlab to work with you, to work the way you work, the way that you like to think.

woodchips
Thanks, I've taken this on board. I think a structure is good advice, and am using them now to store the XYZ matrix and 'bits' associated with the data points.
Bill Cheatham
A: 

I've just had a possible thought about the second half to my question. If I stored the data as an 3 x n x m matrix (instead of n x m x 3), it makes lots of operations easier:

  • Individual coordinates can be extracted with N(:, i, j)
  • All the X, Y or Z coordinates can be extracted as a vector with N(1, :), N(2, :) or N(3, :)
  • All the data can be passed to a function at once
  • It preserves the original format of the data.
  • I can be converted to a 3 x (n*m) matrix easily with N(:, :)

Does this seem like a suitable solution?

Bill Cheatham
Stack Overflow is not a classical forum, do not post this stuff here but edit your original question, please!
Mikhail
Yes, I realize this - I did consider carefully where to post this, but as it is a potential answer to my own question, i suspected it would be better off in the answers section. Or am i not allowed to answer my own question?
Bill Cheatham

related questions