Find values in array by meshgrid coordinate and assign to correct row in table without for loop
4 views (last 30 days)
Show older comments
Hi, I have the the following variables attached below.
XY is a table where each row has a X and Y coordinate of a meshgrid. I would like to take those coordinates and take the corresponding ImRef value at that coordinate and put in the XY.int column.
XY.int already contains the values I want to obtain from ImRef. I did this using a for loop but it takes extremely long and I hope there is a quicker method.
I have tried ismember but it doesn't retain the correct sequence to append it into my table.
Current (slow) approach:
for x = 1:1:height(XY)
XY.int(x) = ImRef(XY.xy(x)==xymesh);
end
EDIT: Added matlab.mat containing the other files I mentioned.
Thanks.
2 Comments
Accepted Answer
Voss
on 6 Mar 2025
"XY is a table where each row has a X and Y coordinate of a meshgrid."
I'll take that to mean that XY.x and XY.y are drawn from two matrices X and Y that have meshgrid format.
"I would like to take those coordinates and take the corresponding ImRef value at that coordinate and put in the XY.ImRef column."
I'll take that to mean that in addition to those X and Y meshgrid matrices I speculated about, you have a third matrix ImRef, of the same size as X and Y, and what you'd like to do is to add a new variable/column called 'ImRef' to the XY table, which column contains values from matrix ImRef where points (XY.x, XY.y) are in matrices (X, Y).
If that's all true, then here's a way to do it:
% first, build X and Y matrices that might or might not be your
% meshgrid-format matrices from which XY.x and XY.y are drawn
XY = load('XY','XY').XY;
UX = unique(XY.x);
UY = unique(XY.y);
[X,Y] = meshgrid(UX,UY);
% take a look at the table (x,y) and the proposed source grid (x,y)
figure
hold on
plot(XY.x,XY.y,'.')
plot(X(:),Y(:),'.','MarkerSize',2)
% make up an ImRef matrix, same size as X and Y
ImRef = X.*Y;
% now find which table (x,y) exist in the meshgrid (x,y) and where
[ism,idx] = ismember([XY.x,XY.y],[X(:),Y(:)],'rows');
% make a new table variable 'ImRef' initially containing all NaNs
XY{:,'ImRef'} = NaN;
% put the values in place from the ImRef matrix
XY.ImRef(ism) = ImRef(idx(ism))
% in this case since I made ImRef = X.*Y, perform a sanity check that
% XY.ImRef == XY.x.*XY.*y, i.e., that the X*Y==ImRef relationship holds
% at the points in the table
isequal(XY.x.*XY.y,XY.ImRef) % good
6 Comments
Voss
on 6 Mar 2025
Checking with the updated mat file:
load('matlab.mat')
whos
[ism,idx] = ismember([XY.x,XY.y],[xmesh(:),ymesh(:)],'rows');
XY{:,'ImRef'} = NaN;
XY.ImRef(ism) = ImRef(idx(ism))
% verify XY.ImRef just populated is the same as int in the original table
XY_old = load('XY.mat','XY').XY;
isequal(XY_old.int,XY.ImRef)
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!