Find values in array by meshgrid coordinate and assign to correct row in table without for loop

4 views (last 30 days)
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
Stephen23
Stephen23 on 6 Mar 2025
What is IMREF? Your data does not contain it:
S = load('XY.mat')
S = struct with fields:
XY: [292x4 table]
T = S.XY
T = 292x4 table
x y xy int _____ _____ _____________ ________ 18.5 -47.5 "18.5,-47.5" 0.078431 -47.5 -18.5 "-47.5,-18.5" 0.10588 5.5 51.5 "5.5,51.5" 0.11373 -51.5 5.5 "-51.5,5.5" 0.11765 -5.5 50.5 "-5.5,50.5" 0.11765 12.5 50.5 "12.5,50.5" 0.12941 51.5 -5.5 "51.5,-5.5" 0.14902 -50.5 -5.5 "-50.5,-5.5" 0.14902 -50.5 12.5 "-50.5,12.5" 0.14902 47.5 18.5 "47.5,18.5" 0.15686 50.5 5.5 "50.5,5.5" 0.16078 5.5 -50.5 "5.5,-50.5" 0.16471 -5.5 -51.5 "-5.5,-51.5" 0.17255 -7.5 50.5 "-7.5,50.5" 0.20392 -18.5 47.5 "-18.5,47.5" 0.25098 -18.5 -48.5 "-18.5,-48.5" 0.27843

Sign in to comment.

Accepted Answer

Voss
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))
XY = 292x5 table
x y xy int ImRef _____ _____ _____________ ________ _______ 18.5 -47.5 "18.5,-47.5" 0.078431 -878.75 -47.5 -18.5 "-47.5,-18.5" 0.10588 878.75 5.5 51.5 "5.5,51.5" 0.11373 283.25 -51.5 5.5 "-51.5,5.5" 0.11765 -283.25 -5.5 50.5 "-5.5,50.5" 0.11765 -277.75 12.5 50.5 "12.5,50.5" 0.12941 631.25 51.5 -5.5 "51.5,-5.5" 0.14902 -283.25 -50.5 -5.5 "-50.5,-5.5" 0.14902 277.75 -50.5 12.5 "-50.5,12.5" 0.14902 -631.25 47.5 18.5 "47.5,18.5" 0.15686 878.75 50.5 5.5 "50.5,5.5" 0.16078 277.75 5.5 -50.5 "5.5,-50.5" 0.16471 -277.75 -5.5 -51.5 "-5.5,-51.5" 0.17255 283.25 -7.5 50.5 "-7.5,50.5" 0.20392 -378.75 -18.5 47.5 "-18.5,47.5" 0.25098 -878.75 -18.5 -48.5 "-18.5,-48.5" 0.27843 897.25
% 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
ans = logical
1
  6 Comments
Voss
Voss on 6 Mar 2025
Checking with the updated mat file:
load('matlab.mat')
whos
Name Size Bytes Class Attributes ImRef 1024x1024 8388608 double ImRes 1x1 8 double XY 292x4 26193 table ans 1x30 60 char xmesh 1024x1024 8388608 double xymesh 1024x1024 66207856 string ymesh 1024x1024 8388608 double
[ism,idx] = ismember([XY.x,XY.y],[xmesh(:),ymesh(:)],'rows');
XY{:,'ImRef'} = NaN;
XY.ImRef(ism) = ImRef(idx(ism))
XY = 292x4 table
x y xy ImRef _____ _____ _____________ ________ 18.5 -47.5 "18.5,-47.5" 0.078431 -47.5 -18.5 "-47.5,-18.5" 0.10588 5.5 51.5 "5.5,51.5" 0.11373 -51.5 5.5 "-51.5,5.5" 0.11765 -5.5 50.5 "-5.5,50.5" 0.11765 12.5 50.5 "12.5,50.5" 0.12941 51.5 -5.5 "51.5,-5.5" 0.14902 -50.5 -5.5 "-50.5,-5.5" 0.14902 -50.5 12.5 "-50.5,12.5" 0.14902 47.5 18.5 "47.5,18.5" 0.15686 50.5 5.5 "50.5,5.5" 0.16078 5.5 -50.5 "5.5,-50.5" 0.16471 -5.5 -51.5 "-5.5,-51.5" 0.17255 -7.5 50.5 "-7.5,50.5" 0.20392 -18.5 47.5 "-18.5,47.5" 0.25098 -18.5 -48.5 "-18.5,-48.5" 0.27843
% 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)
ans = logical
1
Maximilian Rowe
Maximilian Rowe on 6 Mar 2025
Yep, got the same result as well. Concatenating ismember inputs like that with the 'rows' parameter was the key enabler.

Sign in to comment.

More Answers (0)

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!