3

我有以下两个向量场:

>> orient

orient = 

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

>> distance

distance = 

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

我需要取成对元素的叉积,即

b = (cross(orient{1,1},distance{1,1}) + cross(orient{1,2},distance{1,2})..... and so on

然后重塑以匹配距离和方向的尺寸。

我可以在不使用 for 循环的情况下执行此操作吗?

如果我有

orient{1,1} = 

[1x3 double]

distance = 

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

我该怎么办

sum1 = (cross(orient{1,1},distance{1,1}) + cross(orient{1,1},distance{1,2}) +...) sum2 = (cross(orient{1, 2},distance{1,1}) + cross(orient{1,2},distance{1,2}) +...)

其中每个“和”只是单个方向元素的迭代,与距离的所有元素相交,然后将这些叉积​​相加。然后我会有:

mastersum = sum1 sum2 sum3
            sum4 sum5 sum6
            sum6 sum8 sum9

在哪里

sum1 = 

[1x3 double]

我只是把它放在一个令人困惑的方式吗?

4

1 回答 1

4

您需要使用cellfun来遍历没有for-loop 的单元格数组。

对于两个向量场(两个元胞数组),您应该这样做:

crosses = cellfun(@(u, v)cross(u, v)', orient, distance, 'UniformOutput', 0);
b = sum(cell2mat({crosses{:}})', 1)  %# Summing all vectors in all cells

orient来自的单个单元格的类似过程orient{1, 2}是:

u = orient{1, 2};
crosses = cellfun(@(v)cross(u, v)', distance, 'UniformOutput', 0);
b = sum(cell2mat({crosses{:}})', 1)  %# This command remains the same

orient要在没有循环的情况下获取所有向量的结果for,请改为:

b_func = @(u)sum(cell2mat(cellfun(@(v)cross(u,v)', {distance{:}}, 'Un', 0))', 1);
U = cellfun(b_func, orient, 'UniformOutput', 0)

NowU也是一个元胞数组(与 具有相同的维度orient):U{1, 1}具有 、 等的orient{1, 1}交叉U{1, 2}总和orient{1, 2}...

于 2012-07-18T08:59:29.507 回答