Azzera filtri
Azzera filtri

Generate 1 matrix from 3 data columns

24 visualizzazioni (ultimi 30 giorni)
Jess
Jess il 1 Ago 2017
Commentato: Chad Greene il 1 Ago 2017
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.

Risposta accettata

Jan
Jan il 1 Ago 2017
Modificato: Jan il 1 Ago 2017
X = [1,2,3,1,2,3,1,2,3]
Y = [1,1,1,2,2,2,3,3,3]
V = [0.12240, 0.36649, 0.11111, 0.19834, 0.56791, 0.94876, 0.22284, 0.13486, 0.00273]
nX = max(X);
nY = max(Y);
R = zeros(nX, nY);
R(sub2ind(size(R), nY+1-Y, X)) = V;

Più risposte (2)

Geoff Hayes
Geoff Hayes il 1 Ago 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 Commenti
Jess
Jess il 1 Ago 2017
Thanks for the quick response. I'm not sure why but I'm getting an error message saying "Undefined function 'flip' for input arguments of type 'double'." even though the help page for flip specifically lists double under the data types.
Chad Greene
Chad Greene il 1 Ago 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.

Accedi per commentare.


Chad Greene
Chad Greene il 1 Ago 2017
I think you can do this with xyz2grid. Syntax is simply
Data = xyz2grid(Column1,Column2,Column3);
  2 Commenti
Jess
Jess il 1 Ago 2017
Thanks for the quick response, I've never actually used the File Exchange before. Do I need to save the matlab files in the zip folder to a specific directory?
Chad Greene
Chad Greene il 1 Ago 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.

Accedi per commentare.

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by