Converting a 2D nxm Array into appropriate seperate 1D Arrays

Hi,
I seem to struggle with MATLAB alot whenever I have a single 2D array of values that I want to use. Surfacefitting (what I want to use here - e.g. fit(), sftool), ploting etc seem to all want individiual x,y,z column arrays.
Is there a simple way to convert my matrix to these forms?
Many thanks.

 Accepted Answer

Your question is unclear. Are you saying that you have a m-by-3 matrix, but that the function wants to take only 3 column vectors?
If so, simply pass in the columns.
M = rand(20,3); % Your matrix, col1 is x, col2 is y, col3 is z.
func(M(:,1),M(:,2),M(:,3))

3 Comments

Sorry for being unclear - I'm obviosuly having a bit of difficulty in this area.
The Matrix I have is 96-by-192. The function I want to use, despite being designed for surfaces, appears to want to take only 3 column vectors:
"x must be a matrix with either one (curve fitting) or two (surface fitting) columns. For surface fitting, if your data is in separate vectors, then you can use the syntax: fitobject = fit([x,y],z, fitType).
y must be a column vector with the same number of rows as x."
I'm feeling a little lost as to how to get it into this form, if it's even possible.
Many thanks
O.k., so how is your data stored? You say it is 96-by-192, and that is fine, but where are x, y and z stored? A surface has x, y and z coordinates, so you need to figure out how your data is organized before you can proceed.
Sorry for the late response, I didn't see your comment at first.
I think I've fixed it now. My problem indeed was that I didn't have the data organised into x,y,z coordinates and was struggling to do so. I beliive I have solved it now even if I'm still a little uncertain why it works. Practise makes perfect I guess.

Sign in to comment.

More Answers (1)

Hi,
if you know the x and y values (somehow), converting the 2D matrix (which is Z I guess) to a vector would just be
z = Z(:);
If you don't have x and y you might start as well with the fitting, assuming at least that x and y are both with a constant stepsize.
Titus Titus

1 Comment

Thanks for your reply. I think I am a little confused with how these vectors work though.
My x and y can, i think, be arbitrary (my matrix is representing pixel values of an image at each location). For my (m*n) matrix i created an x=1:m and y=1:n and my z as you suggested, but it will not allow me to use these as they are different sizes.
Any clarification would be greatly appreciated as I am getting myself into a muddle.

Sign in to comment.

Tags

Asked:

on 8 Aug 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!