2012-07-03 72 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.您嵌套循环正在做它的方式长度第一维工作,你所访问的单个元素(标量)。你不能用标量穿过标量。