Generate 1 matrix from 3 data columns
조회 수: 24 (최근 30일)
이전 댓글 표시
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
채택된 답변
추가 답변 (2개)
Geoff Hayes
2017년 8월 1일
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
Chad Greene
2017년 8월 1일
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
2017년 8월 1일
Data = xyz2grid(Column1,Column2,Column3);
댓글 수: 2
Chad Greene
2017년 8월 1일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!