Search code examples
matlabmatrixunique

Mapping a column related by unique values to all values in another matrix


I have a matrix of drill hole positions ([X Y]). I have extracted the unique positions ([Xuq Yuq]) and then interpolated an elevation (Z) for each using griddata. Now I want to create a column in the original matrix with the relevant Z assigned back to every X & Y position. Is this possible without for loops?


Solution

  • If you created your unique positions and the elevation by doing something like:

    XY = unique(data(:,[1 2]),'rows');
    Z = f(XY); % some function of XY(:,1) and XY(:,2)
    

    Then all you need to do is keep the third output value from unique and use that to map Z back in appropriately:

    [XY,~,ic] = unique(data(:,[1 2]),'rows');
    Z = f(XY);
    data = [data Z(ic)]; % append the mapped column