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

조회 수: 8 (최근 30일)
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.

채택된 답변

Geoff
Geoff 2012년 5월 18일
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.
  댓글 수: 2
Asim Biswas
Asim Biswas 2012년 5월 18일
This is perfect. I was exactly looking for this. Thank you so so much. I really appreciate your time.
Thank you again. 10% percent helpful.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

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

Steven
Steven 2014년 3월 7일
편집: Steven 2014년 3월 7일
Going through Andrei's solution, I think it is more clear to use this:
[ii,~,i2] = unique(d(:,1));
[jj,~,j2] = unique(d(:,2));

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by