0

嗨,我有以下内容:

for j = 1:N
    for i = 1:N
        vecdir(i,:) = pos(i,:) - pos(j,:);
        dist(i,:) = pdist2(pos(i,:),pos(j,:));
        norm(i,:) = vecdir(i,:)./(dist(i,:)).^2;
    end
    r{j} = norm;

end

u = {or};

我有两个对象 u 和 r:

>> u

u = 

    [9x3 double]

>> r

r = 

  Columns 1 through 8

    [9x3 double]    [9x3 double]    [9x3 double]    [9x3 double]    [9x3 double]    [9x3 double]    [9x3 double]    [9x3 double]

  Column 9

    [9x3 double]

我想取 r 的每个单元格,并取该单元格中每个元素与单元格 u 中的每个元素的叉积,并将结果求和为一个向量大小 [1 3]。到目前为止,我可以使用以下方法一次完成一个(r 的)单元格:

b_func = @(u)nansum(cell2mat(cellfun(@(v)cross(u,v)', {r{1}}, 'Un', 0))', 1);
b = cellfun(b_func, u, 'UniformOutput', 0)

在哪里

b = [1x3 double]

但是我想做的是对 r 中的所有单元格执行此操作,以便最终得到一个对象:

b = 

  Columns 1 through 8

    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]

  Column 9

    [1x3 double]

其中每个单元格都是上面提到的计算,但已经迭代了 r 的每个单元格。任何想法如何做到这一点?

4

2 回答 2

1

怎么样:

b= cellfun(@(x) nansum(cross(u{1},x)),r,'UniformOutput',false);

测试:

>> u = {randi(5,[9 3])}
u = 
    [9x3 double]
>> r = arrayfun(@(x) randi(5,[9 3]),1:8,'UniformOutput',false)
r = 
  Columns 1 through 6
    [9x3 double]    [9x3 double]    [9x3 double]    [9x3 double]    [9x3 double]    [9x3 double]
  Columns 7 through 8
    [9x3 double]    [9x3 double]
>> b = cellfun(@(x) nansum(cross(u{1},x)),r,'UniformOutput',false)
b  = 
  Columns 1 through 6
    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]
  Columns 7 through 8
    [1x3 double]    [1x3 double]
于 2012-08-07T00:09:39.650 回答
0

您可以对其进行整形,使单元格沿第三维方向移动,将其转换为数组并求和:

b = nansum(cell2mat(reshape(cellfun(@(v)cross(u,v), r, 'Un', 0),1,1,[])),1);

然后返回单元格并修复尺寸:

bfinal = shiftdim(mat2cell(b,[1],[3],ones(1,size(b,3))),1);

结果:

>> bfinal

bfinal = 

  Columns 1 through 6

    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]    [1x3 double]

  Columns 7 through 9

    [1x3 double]    [1x3 double]    [1x3 double]
于 2012-08-06T15:20:01.617 回答