# Knowing the value in the first and second column of an array, what is the corresponding value in the third column?

1 view (last 30 days)
Justin Walker on 17 May 2020
Commented: Justin Walker on 20 May 2020
Hello, I've produced a 121x3 array named XYPoints, where the first column corresponds to an X coordinate, the second corresponds to a Y coordinate, and the third column corresponds to the node number at those coordinates.
How would I go about finding the value in the third column if I knew the value in the first two columns? For example, if I knew X=0.05 and Y=0.03, I would have a node number of 15.
width = 0.5;
height = 0.1;
yin = 0.01; %increments in y
xin = 0.05; %increments in x
y=0;
x=0;
nodes=0;
while x<=width
while y<=height
nodes=nodes+1;
X(nodes) = x;
Y(nodes) = y;
y=y+yin;
end
y=0;
x=x+xin;
end
X = X.';
Y = Y.';
XYPoints=[X Y];
XYPoints=[XYPoints,[1:size(XYPoints,1)]']

dpb on 17 May 2020
Edited: dpb on 17 May 2020
Use the MATrix LABoratory features of MATLAB instead of explicit loops where can...
width = 0.5;
height = 0.1;
nIntervalsX = 10; % number elements in x direction
nIntervalsY = 10; % ditto in y
[X Y]=meshgrid(linspace(0,width,nIntervalsX+1),linspace(0,height,nIntervalsY+1)); % 2D X Y grid
XYPoints=[X(:) Y(:) [1:numel(X)].']; % to Nx x Ny x 3 array
fnIDX=@(x,y) find(XYPoints(:,1)==x & XYPoints(:,2)==y); % lookup function
includes a slight generalization of varying number points/elements in the two directions.
>> fnIDX(0.05,0.03)
ans =
15
>>
Works for most cases; in general find is risky for floating point matching, replace the "==" operator with ismembertol
fnIDX=@(x,y)find(ismembertol(XYPoints(:,1),x) & ismembertol(XYPoints(:,2),y));

#### 1 Comment

Justin Walker on 20 May 2020