Mesh grid from 3-column table

6 visualizaciones (últimos 30 días)
Morten Nissov
Morten Nissov el 8 de Abr. de 2021
Editada: Cris LaPierre el 8 de Abr. de 2021
I have a table with values extracted from a csv I want to transform into a grid.
Let's use this table as an example
tdata.x = [1;2;1;2];
tdata.y = [3;3;4;4];
tdata.z = randn(4,1);
tdata=struct2table(tdata);
>> tdata
tdata =
4×3 table
x y z
_ _ _______
1 3 0.53767
2 3 1.8339
1 4 -2.2588
2 4 0.86217
I would like to pivot this into a 2x2 z matrix where rows/columns are given by y and x respectively, something in this direction:
x 1 2
y
3 0.53767 1.8339
4 -2.2588 0.86217
where the first row are the x coordinates, the first columns is the y coordinates and in-between are the corresponding z-values. So that is to say the z-value corresponding to (x,y)=(1,4) is -2.2588.
Note, I am going to use this grid for other things down the road so solutions involving interpolation are not valid, as well the data is guaranteed to be given on a grid.

Respuestas (1)

Cris LaPierre
Cris LaPierre el 8 de Abr. de 2021
Editada: Cris LaPierre el 8 de Abr. de 2021
I think you could achieve something like that using sortrows and reshape.
x = [1;2;1;2];
y = [3;3;4;4];
z = randn(4,1);
tdata=table(x,y,z);
tdata = sortrows(tdata,["x","y"])
tdata = 4×3 table
x y z _ _ ________ 1 3 0.28228 1 4 -0.50085 2 3 1.437 2 4 -0.26357
tmat = array2table(reshape(tdata.z,length(unique(tdata.x)),length(unique(tdata.y))),...
'RowNames',string(unique(tdata.y)),'VariableNames',string(unique(tdata.x)))
tmat = 2×2 table
1 2 ________ ________ 3 0.28228 1.437 4 -0.50085 -0.26357
Now use can use the variable names and row names to access your data from the table. The syntax you elect to use to access the data will determine your input order.
% (rows,variable)
Zval = tmat{"4","1"}
Zval = -0.5008
% Alternate syntax
Zval = tmat.("1")("4")
Zval = -0.5008

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by