2013-02-27 101 views
0

我有一段需要很長時間才能運行的代碼。我閱讀了mathworks網站上的矢量化頁面。我仍然有點困惑,是否可以矢量化我運行plane_intersect的部分?如何在Matlab中正確地進行矢量化

Unvectorized

for N = 1:sizeDimages 
    imPos = anaInfoSat(N).ImagePositionPatient; 
    A2Z = imPos(3); 
    A2Y = imPos(2); 
    A2X = imPos(1); 

    [upP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[uppersatX1,uppersatY1,uppersatZ1],crossS,[A2X,A2Y,A2Z]); 
    [loP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[lowersatX1,lowersatY1,lowersatZ1],crossS,[A2X,A2Y,A2Z]); 
end 

我試圖在量化的事情是UPP1是NX3矩陣。我預先分配upP1矩陣。以下代碼返回有關尺寸不匹配的錯誤。 ImagePosition是一個1x3矩陣。

N = 1:sizeDimages; 
imPos = anaInfoSat(N).ImagePositionPatient; 
A2Z = imPos(3); 
A2Y = imPos(2); 
A2X = imPos(1); 

[upP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[uppersatX1,uppersatY1,uppersatZ1],crossS,[A2X,A2Y,A2Z]); 
[loP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[lowersatX1,lowersatY1,lowersatZ1],crossS,[A2X,A2Y,A2Z]); 

這裏是plane_intersect代碼的一部分,應該足以讓你知道它做什麼。

function [P,N,check]=plane_intersect(N1,A1,N2,A2) 
%plane_intersect computes the intersection of two planes(if any) 
% Inputs: 
%  N1: normal vector to Plane 1 
%  A1: any point that belongs to Plane 1 
%  N2: normal vector to Plane 2 
%  A2: any point that belongs to Plane 2 
% 
%Outputs: 
% P is a point that lies on the interection straight line. 
% N is the direction vector of the straight line 
% check is an integer (0:Plane 1 and Plane 2 are parallel' 
%        1:Plane 1 and Plane 2 coincide 
%        2:Plane 1 and Plane 2 intersect) 
% 
% Example: 
% Determine the intersection of these two planes: 
% 2x - 5y + 3z = 12 and 3x + 4y - 3z = 6 
% The first plane is represented by the normal vector N1=[2 -5 3] 
% and any arbitrary point that lies on the plane, ex: A1=[0 0 4] 
% The second plane is represented by the normal vector N2=[3 4 -3] 
% and any arbitrary point that lies on the plane, ex: A2=[0 0 -2] 
%[P,N,check]=plane_intersect([2 -5 3],[0 0 4],[3 4 -3],[0 0 -2]); 
+0

你可能只是寫'upP1 = pl ane_intersect(...);'。除非你告訴我們'plane_intersect'在做什麼,否則問題的其餘部分是無法回答的。請發佈代碼。 – shoelzer 2013-02-27 18:36:03

+0

我用plane_intersect info/code更新了問題。 – 2013-02-27 18:44:30

回答

3

在您的矢量化代碼anaInfoSat(N).ImagePositionPatient;將不會返回一個單一的,但幾個答案。如果將分鐘分配給單個變量,它將只收到第一個答案。這就是爲什麼你會得到尺寸不匹配錯誤。

根據數據類,你可以組合成一個矩陣

imPos = [anaInfoSat(N).ImagePositionPatient]; 

或進入一個單元陣列

imPos = {anaInfoSat(N).ImagePositionPatient}; 

您也可以分配給多個變量同時:

[A2X, A2Y, A2Z] = anaInfoSat(1:3).ImagePositionPatient;