2017-03-06 5 views
0

Ich Vectoring tatsächlich einen meiner Code und ich habe ein paar Probleme.Matlab: Vectorize Technik in 3 Dimensionen Matrix

Dies ist mein erster Code:

CoordVorBd = random(N+1,3) 
CoordCP = random(N,3) 
v = random(1,3) 
for i = 1 : N 
    for j = 1 : N 
      ri1j = (-CoordVorBd (i,:) + CoordCP(j,:)); 

      vij(i,j,:) = cross(v,ri1j))/(norm(ri1j) 
    end  

end 

beginne ich habe zu vektorisiert, dass einige Matrizen erstellen, die 3 * 1 Vektoren enthält. Meine Größe der Matrix ist N * N * 3.

CoordVorBd1(1:N,:) = CoordVorBd(2:N+1,:); 
CoordCP_x= CoordCP(:,1); 
CoordCP_y= CoordCP(:,2); 
CoordCP_z= CoordCP(:,3); 
CoordVorBd_x = CoordVorBd([1:N],1); 
CoordVorBd_y = CoordVorBd([1:N],2); 
CoordVorBd_z = CoordVorBd([1:N],3); 
CoordVorBd1_x = CoordVorBd1(:,1); 
CoordVorBd1_y = CoordVorBd1(:,2); 
CoordVorBd1_z = CoordVorBd1(:,3); 
[X,Y] = meshgrid (1:N); 

ri1j_x = (-CoordVorBd_x(X) + CoordCP_x(Y)); 
ri1j_y = (-CoordVorBd_y(X) + CoordCP_y(Y)); 
ri1j_z = (-CoordVorBd_z(X) + CoordCP_z(Y)); 

ri1jmat(:,:,1) = ri1j_x(:,:); 
ri1jmat(:,:,2) = ri1j_y(:,:); 
ri1jmat(:,:,3) = ri1j_z(:,:); 
vmat(:,:,1) = ones(N)*v(1); 
vmat(:,:,2) = ones(N)*v(2); 
vmat(:,:,3) = ones(N)*v(3); 

Dieser Code funktioniert, aber ist in Bezug auf variable Erstellung schwer. Ich habe es nicht geschafft, die Vektorisierung gleichzeitig auf alle Matrizen anzuwenden.

Formule wie

ri1jmat(X,Y,1:3) = (-CoordVorBd (X,:) + CoordCP(Y,:)); 

funktioniert nicht ... Wenn jemand ein paar Ideen haben etwas sauberer zu haben.

An diesem Punkt habe ich eine N * N * 3 Matrix ri1jmat mit allen meinen Vektoren.

Ich will die N * N rij1norm Matrix berechnen, die die Norm der Vektoren ist

rij1norm(i,j) = norm(ri1jmat(i,j,1:3)) 

Lage sein, die vij Matrix vektorisieren.

vij(:,:,1:3) = (cross(vmat(:,:,1:3),ri1jmat(:,:,1:3))/(ri1jmatnorm(:,:)); 

Das Kreuzprodukt funktioniert.

Ich versuchte Zahlen der Methode ohne zu erreichen, diese Rij1norm Matrix ohne eine doppelte Schleife zu haben.

Wenn jemand ein paar Tricks hat, danke von vorne.

Antwort

0

Hier ist eine vektorisierte Version. Beachten Sie, dass Ihre ursprüngliche Schleife die letzte Spalte von CoordVorBd nicht enthielt. Wenn dies also beabsichtigt war, müssen Sie sie auch aus dem folgenden Code entfernen. Ich nahm an, dass es ein Fehler war.

CoordVorBd = rand(N+1,3); 
CoordCP = rand(N,3); 
v = rand(1,3); 

repCoordVor=kron(CoordVorBd', ones(1,size(CoordCP,1)))'; %based on http://stackoverflow.com/questions/16266804/matlab-repeat-every-column-sequentially-n-times 
repCoordCP=repmat(CoordCP, size(CoordVorBd,1),1); %repeat matrix 
V2=-repCoordVor + repCoordCP; %your ri1j 
nrm123=sqrt(sum(V2.^2,2)); %vectorized norm for each row 
vij_unformatted=cat(3,(v(:,2).*V2(:,3) - V2(:,2).*v(:,3))./nrm123,(v(:,3).*V2(:,1) - V2(:,3).*v(:,1))./nrm123,(v(:,1).*V2(:,2) - V2(:,1).*v(:,2))./nrm123); % cross product, expanded, and each term divided by norm, could use bsxfun(@rdivide,cr123,nrm123) instead, if cr123 is same without divisions 
vij=permute(reshape(vij_unformatted,N,N+1,3),[2,1,3]); %reformat to match your vij 
0

Hier ist eine weitere Möglichkeit, es zu tun arrayfun mit

% Define a meshgrid of indices to run over 
[I, J] = meshgrid(1:N, 1:(N+1)); 
% Calculate ril for each index 
rilj = arrayfun(@(x, y) -CoordVorBd (y,:) + CoordCP(x,:), I, J, 'UniformOutput', false); 
%Calculate vij for each point 
temp_vij1 = arrayfun(@(x, y) cross(v, rilj{x, y})/norm(rilj{x, y}), J, I, 'UniformOutput', false); 
%Reshape the matrix into desired format 
temp_vij2 = cell2mat(temp_vij1); 
vij = cat(3, temp_vij2(:, 1:3:end), temp_vij2(:, 2:3:end), temp_vij2(:, 3:3:end)); 
Verwandte Themen