필터 지우기
필터 지우기

reconstruct matrix from vector coordinates

조회 수: 1 (최근 30일)
Nicolas
Nicolas 2017년 1월 24일
댓글: Walter Roberson 2017년 1월 25일
Hello, I have 3 vectors (x,y,data) each equals 1x6358. How can I recreate the matrix DATA based on the indices x and y? Thank you

채택된 답변

Walter Roberson
Walter Roberson 2017년 1월 25일
Possibly you should be using scatteredInterpolant() or TriScatteredInterp to create an interpolant that you would then sample at points on a 2D grid.
Or possibly you should use
[xu, ~, xuidx] = unique(x);
[yu, ~, yuidx] = unique(y);
nxu = length(xu);
nyu = length(yu);
DATA = accumarray( [xuidx(:), yuidx(:)], data(:) );
This would be primarily for the situation where your x and y values form a grid but not necessarily of integer values and not necessarily equi-distant. This particular form of the code expects that all the x values that are intended to be treated as equal are exactly equal -- for example although 1 and 1-eps both display as 1, they are not exactly equal and this code would treat them as different. (If you do have a grid but the values are not exactly equal then there are ways to deal with that, such as by using uniquetol())
  댓글 수: 2
Nicolas
Nicolas 2017년 1월 25일
Yes, the mesh is more compact in some areas than some others.
Walter Roberson
Walter Roberson 2017년 1월 25일
The nxu and nyu lines above are not needed; I forgot to edit those out. You can get away with
[xu, ~, xuidx] = unique(x);
[yu, ~, yuidx] = unique(y);
DATA = accumarray( [xuidx(:), yuidx(:)], data(:) );
or, if needed, uniquetol() instead of unique()
This would produce a grid that would not necessarily be equally spaced. The vectors xu and yu give the actual coordinates.
If you wanted to create an equally spaced mesh, then probably after the above you would use griddedInterpolant together with a mesh of the points you wanted to sample at.

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

추가 답변 (1개)

Stephen23
Stephen23 2017년 1월 24일
편집: Stephen23 2017년 1월 24일
You could use sub2ind:
sz = [max(x),max(y)];
ix = sub2ind(sz,x,y);
mat = NaN(sz);
mat(ix) = data;
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 1월 25일
Your x and y are not indices in the MATLAB sense.
Nicolas
Nicolas 2017년 1월 25일
no they are spatial coordinates from the mesh of a numerical model. do I need to find a way to create the indices from the mesh in spatial coordinates?

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

카테고리

Help CenterFile Exchange에서 Computational Geometry에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by