Monday, March 08, 2010

Matlab: column circshift by each row

Q:
Suppose T is a triangular mesh, f is the face list, v is the vertex list;

For some vertex k, I have the info of its neighboring faces va, which is an index into f.

fva = f(va,:) is length(va) by 3 matrix about vertices involved in va, and k is present in every row of fva.

I want to re-arrange columns in fva, row by row, so that vertex k is always in the first column, while the rotation order of vertices (clockwise,cc) is retained for each row.

% Set up the data;
load seamount;
v = [x,y,z];
f = delaunay(x,y);
k = 10;
va = find(any(f == 10,2));
fva = f(va,:);


A1: straightforward for-loop solution;

for c = 1:length(va)
if fva(c,2) == k, fva(c,:) = fva(c,[2 3 1]); end
if fva(c,3) == k, fva(c,:) = fva(c,[3 1 2]); end
end
A2:

fvaT = fva.';
[ii,~] = find(fvaT == 10);
cOrder = [1 2 3; 2 3 1; 3 1 2];
fva = fvaT(bsxfun(@plus,cOrder(:,ii),(0:3:numel(ii)*3-3))).';
Generally A1 is faster than A2;

No comments: