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:
load mandrill
Z = X;
nrows = size(Z,1); ncols = size(Z,2);
[x1,y1] = meshgrid(1:nrows,1:ncols);
x = x1(:); y = y1(:); Z = Z(:);
index = randperm(numel(Z));
x = x(index); y = y(index); Z = Z(index);
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.
Best Answer