Meshgrid

23 views (last 30 days)
Charles
Charles on 27 Jan 2011
Hi,
I have 3 vectors x,y,z; with x and y representing co-ordinates and z their values. Picture this as an image matrix that has been decomposed into vectors of x & y and pixel intensity z. Now I want to get back my picture. Here is what I did and where I got stuck:
[x1 y1] = meshgrid(-a:a) % -a:a defining the square image.
Now the problem is how to assign values (z) at the appropriate co-ordinates. Suggestions would be appreciated.
Charles

Accepted Answer

Andrew Newell
Andrew Newell on 27 Jan 2011
I assume that x and y have the same values as x1 and y1, but in a different order. You could do a search for each pair (x1,y1), but it's faster to sort the coordinates and then reshape Z. Here is an example with an actual image to give you the idea:
%%Set up the problem
load mandrill
Z = X; %rename image
nrows = size(Z,1); ncols = size(Z,2);
[x1,y1] = meshgrid(1:nrows,1:ncols);
x = x1(:); y = y1(:); Z = Z(:);
% Scramble the elements.
index = randperm(numel(Z));
x = x(index); y = y(index); Z = Z(index);
%%Now the problem is set up, unscramble x and y and then reshape Z.
xy = [x y];
[~,index] = sortrows(xy,[1 2]);
x = x(index); y = y(index); Z = Z(index);
Z = reshape(Z,nrows,ncols);
image(Z)
Of course, if x and y were originally obtained from a process like that in the previous cell, you won't need to sort.

More Answers (1)

Ashish Uthama
Ashish Uthama on 27 Jan 2011
Either use interp2, griddata or if you have a newer version, triscatteredinterpclass. The doc pages have examples to get you started.

Tags

Community Treasure Hunt

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

Start Hunting!