identify a factor that links two equal matrices but positioned in different places on the same plane
    5 views (last 30 days)
  
       Show older comments
    
I have two matrices ('RC_matlab' and 'RC_trasl') representing the same 2D coordinates of nodes on the same plane but at different plane positions.
I would like to identify the factor(s) that links the 'RC_trasl' matrix to the 'RC_matlab' matrix. That is, transform 'RC_trasl' as 'RC_matlab'.
I tried to determine the centroid (with a function found among the answers in the forum) of the two matrices in order to obtain dX and dY but this is not the correct way (apparently). 
load RC_matlab.mat
load RC_trasl.mat
% polygoncentroid
[xc_matlab,yc_matlab] = polygoncentroid(RC_matlab(:,1),RC_matlab(:,2));
[xc_trasl,yc_trasl] = polygoncentroid(RC_trasl(:,1),RC_trasl(:,2));
dX = xc_matlab - xc_trasl;
dY = yc_matlab - yc_trasl;
% Plot
figure
plot(RC_matlab(:,1),RC_matlab(:,2),'b.','Markersize',30);
hold on
plot(xc_matlab,yc_matlab,'k*','Markersize',10);
hold off
axis equal
figure
plot(RC_trasl(:,1),RC_trasl(:,2),'r.','Markersize',30);
hold on
plot(xc_trasl,yc_trasl,'k*','Markersize',10);
hold off
axis equal
% New matrix
R_trasl_new = dX + RC_trasl(:,1);
C_trasl_new = dY + RC_trasl(:,2);
RC_trasl_new = [R_trasl_new , C_trasl_new];
% ===== function
function [xc,yc] = polygoncentroid(x,y)
    xn = circshift(x,-1);
    yn = circshift(y,-1);
    A = x.*yn-xn.*y;
    a = 3*sum(A);
    xc = sum((x+xn).*A)/a;
    yc = sum((y+yn).*A)/a;
end

0 Comments
Accepted Answer
  Voss
      
      
 on 5 Oct 2023
        
      Edited: Voss
      
      
 on 5 Oct 2023
  
      load RC_matlab
load RC_trasl
% calculate the centroid of each set of points:
c_matlab = mean(RC_matlab,1);
c_trasl = mean(RC_trasl,1);
% calculate the offset of each point from its set's centroid:
d_matlab = RC_matlab-c_matlab;
d_trasl = RC_trasl-c_trasl;
% find the scale of the "matlab" set of points relative to the "trasl" set of points,
% by finding the mean ratio of the distances to their respective centroids:
scale_vector = sqrt(sum(d_matlab.^2,2))./sqrt(sum(d_trasl.^2,2));
scale = mean(scale_vector(scale_vector~=0 & isfinite(scale_vector)),1); % remove 0s, NaNs, and Infs, which can happen when a point is at the centroid
% then each  can be (approximately) constructed from the other by a linear transformation:
RC_matlab_recon = (RC_trasl-c_trasl)*scale+c_matlab;
RC_trasl_recon = (RC_matlab-c_matlab)/scale+c_trasl;
% Plot
figure
plot(RC_matlab(:,1),RC_matlab(:,2),'b.','Markersize',30);
hold on
plot(RC_matlab_recon(:,1),RC_matlab_recon(:,2),'g*','Markersize',10);
hold off
axis equal
figure
plot(RC_trasl(:,1),RC_trasl(:,2),'r.','Markersize',30);
hold on
plot(RC_trasl_recon(:,1),RC_trasl_recon(:,2),'y*','Markersize',10);
hold off
axis equal
% check the max difference between the original and the reconstruction:
max(abs(RC_matlab-RC_matlab_recon),[],'all')
max(abs(RC_trasl-RC_trasl_recon),[],'all')
4 Comments
  Voss
      
      
 on 17 Oct 2023
				"Is there a way to transform RC_transl into RC_matlab_recon?"
Yes, exactly as I said in my comment before (and demonstrated in my answer before that):
RC_matlab_recon = (RC_trasl-c_trasl)*scale+c_matlab;
You can't do it with only a scalar multiplicative parameter, but that expression includes a multiplicative parameter (scale). The expression is translating RC_trasl by its centroid (so that the points become centered at the origin), then applying the multiplicative factor (scale), then translating such that the centroid is the centroid of  RC_matlab_recon (which is c_matlab).
You could do the same thing with a linear transformation matrix, and maybe that's what you have in mind. https://en.wikipedia.org/wiki/Transformation_matrix
More Answers (0)
See Also
Categories
				Find more on Creating and Concatenating Matrices 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!


