You just have to rearrange the values so that each point has its four coordinates in one row:
result = [X_old(:) Y_old(:) Z_old(:) ones(numel(X_old),1)] * M;
Each row of result
gives the new coordinates of each point.
Note that this works because multiplying matrix A
times matrix B
is the same as multiplying each row of A
times B
.
Or, if you can't afford to build the above matrix (because of memory limitations), use
result = X_old(:)*M(1,:) + Y_old(:)*M(2,:) + Z_old(:)*M(3,:) + ones(numel(X_old),1)*M(4,:);
In either case, if you want to rearrange the result so that it has the same size as X_old
, Y_old
, Z_old
, use this as a last step:
s = size(X_old);
X_new = reshape(result(:,1), s);
Y_new = reshape(result(:,2), s);
Z_new = reshape(result(:,3), s);