我怎样才能应用math计算每个元素的单元格数组(如在Excel中)
朋友们,
我有两个以下的单元格arrays(或两个Excel表,如有必要)。
细胞arrays的Bodies
[Body] [Weigh] [warranty] [A1] [3.5] [2] [A2] [6.2] [3] [B1] [7.1] [1] [B2] [3.9] [4] [B3] [4.2] [5] [C1] [1.3] [7] ... .... ...
用于每个Body
的部分Objects
单元格数组
[Object] [x_min] [x_max] [y_min] [y_max] [Volume] [A1-a1] [5.7] [7.3] [8.9] [4.3] [5.1] [A1-a2] [2.6] [5.6] [9.3] [5.7] [5.2] [A1-a3] [3.6] [7.3] [5.3] [7.3] [5.8] [A2-a1] [8,2] [5.8] [2.7] [5.3] [6.3] [A2-a2] [8.4] [6.3] [8.5] [6.3] [9.3] [B1-b1] [7.1] [6.3] [8.2] [8.5] [5.3] [B1-b2] [8.9] [8.4] [4.5] [6.2] [4.5] [C1-c1] [7.0] [7.1] [1.3] [8.9] [1.3] [C1-c2] [6.9] [4.8] [3.2] [9.2] [3.7] [C1-c3] [5.3] [2.5] [4.2] [6.4] [6.3] ... ... ... ... ... ...
我想写一个程序,它会自动执行以下步骤到对象:
-
根据公式计算每个对象的权重:
Weigh_obj = Weigh_body * Volume_obj / Sum of every Volume_obj in the body
例如
Weigh_A1-a1 = Weigh_A1 * Volume_A1-a1 / (Volume_A1-a1 + Volume_A1-a2 + Volume_A1-a3) = 3.5 * 5.1 / (5.1+5.2+5.8)
- 比较每个对象的协调与其他对象的协调以找出,如果在任何两个对象之间有一个
Touch
:
例如,有两个Objects
Object1
和Object2
:
if ( (x2_min <=x1_min<=x2_max)or(x2_min <=x1_max<=x2_max)... and (y2_min <=y1_min<=y2_max)or(y2_min <=y1_max<=y2_max) ) % returns '1' in corresponding positions in the square matrix of `n` objects, like this: [ X] [O1] [O2] [O3] ... [O_n] [O1] [X] [1] [ ] ... [O2] [1] [X] [ ] ... [O3] [ ] [ ] [X] ... ... ... ... ... [X] [On] ... ... ... ... [X] else returns '0' in the corresponding Positions in the matrix end
所以最后我得到一个完整的Object
matrix
我希望我已经清楚地解释了我的问题。
提前致谢!
非常感谢你的帮助 !
对于1)步骤:collsion检测:我想检测第二个表格中任意两个对象之间的折叠。不仅A1-a1与A1-a2 …,还有A1-a1与B1-b1 …, C1-C1 …等等。 之后,我想要build立一个像这样的matrix
[ X] [A1-a11] [A1-a2] [A1-a3] ... [B1-b1] [B1-b2] [C1-c1] [A1-a1] [X] [ ] [ ] ... [ ] [ ] [ ] [A1-a2] [ ] [X] [ ] ... [A1-a3] [ ] [ ] [X] ... ... ... ... ... [X] [B1-b1] ... ... ... ... [X] [B1-b2] [C1-c1] ... ... ... ... .. ... [X]
并将该matrix填充到collsion检测的结果。 你有什么想法吗?
对于2)步骤,你已经确定了我需要的东西。 但是在这里我们没有findobjets的密度。 我们只是find对象的重量。 因为你用公式计算“具体量”:
[object volume / total body volume] = [m^3 / m^3] = [1] (no unit, just a quote)
最后[体重*特定体积] = [kg * 1] = [kg] = [物体的重量]
问候 !
如果我正确地理解了你,你想做碰撞检测和计算单个物体的密度。
相信与否,碰撞检测比容积计算要容易得多。 这主要是由于你的数据是如何组织的。
这是我到达的地方:
% [Body] [Weight] [warranty] Bodies = {... 'A1' 3.5 2 'A2' 6.2 3 'B1' 7.1 1 'B2' 3.9 4 'B3' 4.2 5 'C1' 1.3 7 }; % [Object] [x_min] [x_max] [y_min] [y_max] [Volume] Objects = {... 'A1-a1' 5.7 7.3 8.9 4.3 5.1 'A1-a2' 2.6 5.6 9.3 5.7 5.2 'A1-a3' 3.6 7.3 5.3 7.3 5.8 'A2-a1' 8.2 5.8 2.7 5.3 6.3 'A2-a2' 8.4 6.3 8.5 6.3 9.3 'B1-b1' 7.1 6.3 8.2 8.5 5.3 'B1-b2' 8.9 8.4 4.5 6.2 4.5 'C1-c1' 7.0 7.1 1.3 8.9 1.3 'C1-c2' 6.9 4.8 3.2 9.2 3.7 'C1-c3' 5.3 2.5 4.2 6.4 6.3 }; % Rename variables for clarity BodyNames = Bodies(:,1); Weights = cat(1, Bodies{:,2}); ObjectNames = Objects(:,1); x_min = [Objects{:,2}].'; x_Max = [Objects{:,3}].'; y_min = [Objects{:,4}].'; y_Max = [Objects{:,5}].'; Volume = [Objects{:,6}].'; % Find densities % -------------------- % find which objects belong to which bodies ObjInds = cellfun(@(x) regexp(ObjectNames, x), BodyNames, 'UniformOutput', false); ObjInds = cellfun(@(x) ~cellfun('isempty', x), ObjInds, 'UniformOutput', false); % Compute the specific volumes (object volume / total body volume) specificVolumes = cellfun(@(x) Volume(x) ./ sum(Volume(x)), ObjInds, 'UniformOutput', false); % Compute densities (= body weight * specificVolumes) densities = cellfun(@(x,y)x.*y, num2cell(Weights), specificVolumes, 'UniformOutput', false); densities = cat(1, densities{:}); % Collsion detection % -------------------- % This is it: Colissions = ... bsxfun(@le, x_min, x_Max') & bsxfun(@ge, x_min, x_min') & ... bsxfun(@le, y_min, y_Max') & bsxfun(@ge, y_min, y_min');
现在,上面的碰撞检测会给出一些“奇怪”的结果。 这是由于你的一些x_min > x_max
和一些y_min > y_max
。 这很容易用sort()
或类似的方法纠正,但是我会把它留给你。