matlab中多行(点)的平均值

时间:2015-05-12 12:19:23

标签: matlab mean

我有一个名为x,y,z位置数据的矩阵。 我使用pdist计算了所有点之间的距离,得到了矩阵'out',它具有x的diff,y的diff和z的diff以及它们对应的行。

A= data;
D1= pdist(A(:,1));
D2= pdist(A(:,2));
D3= pdist(A(:,3));
D = [D1' D2' D3'];


tmp = ones(size(A,1));
tmp = tril(tmp,-1);
[rowIdx,colIdx ] = find(tmp);
out = [D,A(rowIdx,:),A(colIdx,:)];

我想计算满足某些条件的所有点的平均值: diff z< 7和diff x和diff y< 4。

所以我写了下面的代码:

a= find (out(:,3)>0);
cal=out(a,:);
b= cal(:,3)<7;
cal2 = cal(b,:);

[s,k]= size (cal2);


for i=1:s

    if (cal2(i,1) < 4) && (cal2(i,2) < 4);

        xmean = mean (cal2(i,[4,7]));
        ymean = mean (cal2(i,[5,8]));
        zmean = mean (cal2(i,[6,9]));
        fd = [xmean ymean zmean];


    end
end

问题是,通过这段代码,我可以一次得到两点的平均值。所以,我的输出给了我比我想要的更多的分数。我想得到满足条件的所有点的平均值。

我的最终目标是得到一个点列表,在那里我可以获得其(diff z> 7)+的点(diff z <7的点的平均值,而diff x&lt; 4和diff y <4)+(diff z <7但diff x和diff y> 4)。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

假设您想要所有点的平均值,其中diffZ&lt; 7,diffX&lt; 4和diffY&lt; 4。假设out存储前三列中x,y,z的相应差异,以及列4-9中的点坐标。

%# this is true for all points you want to average
condition = out(:,1) > 4 & out(:,2) > 4 & out(:,3) < 7;

%# get coordinates satisfying the condition; create a 2n-by-3 array
coords = [out(condition, 4:6);out(condition, 7:9)];

%# if you don't want to count coordinates multiple times when taking the average, run 
%# coords = unique(coords,'rows');

%# average
meanCoordinates = mean(coords,1);

答案 1 :(得分:0)

A = [0,0,0;0,0,5;0,0,10];

% create matrices with the distances 
% [p1-p1, p1-p2, p1-p3;
%  p2-p1, p2-p2, p2-p3;
%  p3-p1, p3-p2, p3-p3];
dx= squareform(pdist(A(:,1)));
dy= squareform(pdist(A(:,2)));
dz= squareform(pdist(A(:,3)));
% dz = [0,5,10;5,0,5;10,5,0];

% select all points stisfying condition
idx = dx < 4 & dy < 4 & dz < 7;
% remove entries of the main diagonal
idx(sub2ind(size(idx),1:size(A,1),1:size(A,1))) = 0;
% select all indices. Since all distances are 2 times in the full matrix,
% only r or c is needed
[r,c] = find(idx==1);
% r = [2,1,3,2]; c = [1,2,2,3]

meanOfPoints = mean(A(r,:))