2012-07-03 75 views
0

我遇到了跨產品功能的問題。我需要爲每個像素取兩個向量的叉積,然後求和所有像素的結果。matlab - 交叉產品錯誤

i=1;  
[h,w,d] = size(current_vec); 
for pxRow = 1:h % fixed pixel row 
for pxCol = 1:w % fixed pixel column 
for pxsize = 1:d 

for r = 1:h % row of distant pixel 
for c = 1:w % column of distant pixel 
for dpth = 1:d 

bfield(c,r,dpth) = cross(current_vec(c,r,dpth),dist_vec(c,r,dpth));        % pythagoras theorem to get distance to each pixel                  % unit vector from x to s      

end 
end 
end 
Bfield(i) = {bfield}; % filling a cell array with results. read below 
i = i+1; 
end 
end 
end 


??? Error using ==> cross at 37 
A and B must have at least one dimension of length 3. 

??? Error using ==> cross at 37 
A and B must have at least one dimension of length 3. 

Error in ==> MAC2 at 71 
bfield(c,r,dpth) = cross(current_vec(c,r,dpth),dist_vec(c,r,dpth));        

但是違規的載體current_vec和dist_vec如下:

>> size(current_vec) 

ans = 

    35 35  3 

>> size(dist_vec) 

ans = 

    35 35  3 

所以就我而言,他們填補了跨產品使用的標準。爲什麼不是這種情況?

+0

在調用'cross'時,使用'current_vec(c,r,dpth)'和'dist_vec(c,r,dpth)',它們只是標量,而不是矢量。 –

回答

3

您需要使用的cross的矢量形式:

bfield = cross(current_vec,dist_vec); 

cross將與3.您嵌套循環正在做它的方式長度第一維工作,你所訪問的單個元素(標量)。你不能用標量穿過標量。