matlab-带叉积的误差
我对交叉积函数有问题。我需要对每个像素取两个向量的叉积,然后对所有像素的结果求和matlab-带叉积的误差,matlab,cross-product,Matlab,Cross Product,我对交叉积函数有问题。我需要对每个像素取两个向量的叉积,然后对所有像素的结果求和 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
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));
但是,当前_vec和距离_vec的冲突向量如下:
>> size(current_vec)
ans =
35 35 3
>> size(dist_vec)
ans =
35 35 3
因此,就我而言,它们符合交叉积中使用的标准。为什么不是这样呢?您需要使用
交叉的矢量化形式:
bfield = cross(current_vec,dist_vec);
cross
将用于长度为3的第一个维度。使用嵌套循环的方式是,通过索引访问单个元素(标量)。不能将标量与标量交叉。您需要使用矢量化形式的交叉:
bfield = cross(current_vec,dist_vec);
cross
将用于长度为3的第一个维度。使用嵌套循环的方式是,通过索引访问单个元素(标量)。不能将标量与标量交叉。在调用交叉
时,使用当前向量(c,r,dpth)
和距离向量(c,r,dpth)
,它们只是标量,而不是向量。在调用交叉时,使用当前向量(c,r,dpth)
和距离向量(c,r,dpth)
,它们只是标量,而不是向量。