Generate 1 matrix from 3 data columns
45 views (last 30 days)
Show older comments
I need advice and hopefully someone out there can help me. I have 3 columns of numerical data with n rows. All three columns will always have the same number of rows, but this can range between 2 and 10000 for a given data set. The example shows the kind of data I'm working with, where the first column lists X-coordinates, the second column lists Y-coordinates, and the third column lists the measured value at that x,y pair.
Example data:
X-Coord Y-Coord Data
1.00000 1.00000 0.12240
2.00000 1.00000 0.36649
3.00000 1.00000 0.11111
1.00000 2.00000 0.19834
2.00000 2.00000 0.56791
3.00000 2.00000 0.94876
1.00000 3.00000 0.22284
2.00000 3.00000 0.13486
3.00000 3.00000 0.00273
What I want is to turn Column 3 into a matrix like the one below
Data Output:
0.22284 0.13486 0.00273
0.19834 0.56791 0.94876
0.12240 0.36649 0.11111
I feel like it should be possible and relatively simple, but I'm running into trouble. I tried starting by building a square matrix, and filling it point by point but with more rows:
Data = zeros(3)
Data(1,1) = Data(7)
Data(2,1) = Data(4)
Data(3,1) = Data(1)
but this becomes cumbersome and time consuming as the number of rows increase. I was considering building a loop to read X, Y, and Data and fill the matrix point by point, but I'm not sure where to begin with that.
0 Comments
Accepted Answer
More Answers (2)
Geoff Hayes
on 1 Aug 2017
Jess - if z is your third column, then you could do something like
flip(reshape(z, [3 3])',1)
We re-shape z from a 9x1 to a 3x3 array. We then transpose it (with the apostrophe) and then flip it in the first dimension to flip the order of elements.
i.e.
>> z [
0.1224
0.36649
0.11111
0.19834
0.56791
0.94876
0.22284
0.13486
0.00273];
>> flip(reshape(z, [3 3])',1)
ans =
0.22284 0.13486 0.00273
0.19834 0.56791 0.94876
0.1224 0.36649 0.11111
2 Comments
Chad Greene
on 1 Aug 2017
The flip function was introduced in 2013b. If you have an older version you can do
flipud(reshape(z,[3 3])')
But with that solution you have to be careful that the x and y coordinates are perfectly regular.
Chad Greene
on 1 Aug 2017
Data = xyz2grid(Column1,Column2,Column3);
2 Comments
Chad Greene
on 1 Aug 2017
Hi Jess,
Yeah, unzip the folder and put it somewhere Matlab can find it. There are a number of ways to do that, but the easiest is just to put the xyz2grid.m file in your current working directory.
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!