How to create a regular data matrix from irregularly sampled XYZ data with different X and Y coordinates

8 views (last 30 days)
Dear, I am trying to organise my data in a matrix. The data is in the form of XYZ, where X and Y are irregular. For example a data structure like below
X Y Z
2 0 0.38
2 5 0.348
2 25 0.328
3 2 0.319
3 50 0.212
5 10 0.138
5 60 0.11
5 100 0.087
7 5 0.069
7 9 0.4113
7 45 0.3807
7 95 0.3678
9 10 0.3207
9 35 0.1765
15 65 0.1123
15 75 0.0917
15 99 0.0727
15 125 0.0573
15 150 0.3811
I have a number of Y data points for a particular X. X has a different range from Y and both has irregular intervals. I want to organize the data in a matrix, where X may be in column heading, Y may in Row heading and the matrix elements are the Z data. My target is to extract Z data for a particular X value with different Y or a particular Y with different X. Can anyone please help me. Your help is highly appreciated. Thank you so much.

Accepted Answer

Geoff
Geoff on 18 May 2012
Well, I might do this:
d = [2.0000 0 0.3800;
2.0000 5.0000 0.3480;
2.0000 25.0000 0.3280;
3.0000 2.0000 0.3190;
3.0000 50.0000 0.2120;
5.0000 10.0000 0.1380;
5.0000 60.0000 0.1100;
5.0000 100.0000 0.0870;
7.0000 5.0000 0.0690;
7.0000 9.0000 0.4113;
7.0000 45.0000 0.3807;
7.0000 95.0000 0.3678;
9.0000 10.0000 0.3207;
9.0000 35.0000 0.1765;
15.0000 65.0000 0.1123;
15.0000 75.0000 0.0917;
15.0000 99.0000 0.0727;
15.0000 125.0000 0.0573;
15.0000 150.0000 0.3811];
X = d(:,1); Y = d(:,2); Z = d(:,3);
Xs = unique(X);
Ys = unique(Y);
Xi = arrayfun( @(x) find(Xs==x), X );
Yi = arrayfun( @(y) find(Ys==y), Y );
Li = Yi + (Xi-1) * numel(Ys);
XYZ = nan(numel(Ys), numel(Xs));
XYZ( Li ) = Z;
That creates a matrix where each column represents a value in Xs and each row represents a value in Ys, and the Z values are injected accordingly using linear indexing.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 18 May 2012
[ii,i2,i2] = unique(d(:,1));
[jj,j2,j2] = unique(d(:,2));
out = [[NaN,ii'];[jj,accumarray([j2,i2],d(:,3),[],[],NaN)]]

Steven
Steven on 7 Mar 2014
Edited: Steven on 7 Mar 2014
Going through Andrei's solution, I think it is more clear to use this:
[ii,~,i2] = unique(d(:,1));
[jj,~,j2] = unique(d(:,2));

Community Treasure Hunt

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

Start Hunting!