矩阵内的操作避免循环
Operations within matrix avoiding for loops
我有一个相当简单的问题,但我无法在 MATLAB 中得到正确的结果。
我正在 Matlab 中编写代码,其中有一个 200x3 矩阵。这个数据对应10个不同点的记录,我每个点都拍了20帧。
这只是为了解决测量系统中的误差。所以现在我想通过计算测量的独立坐标的平均值来计算这个矩阵中每个点的 3D 坐标。
一个例子(1 个点有 3 个测量值)是:
MeasuredFrames (Point 1) =
x y z
1.0000 2.0000 3.0000
1.1000 2.2000 2.9000
0.9000 2.0000 3.1000
Point = mean(MeasuredFrames(1:3, :))
Point =
1.0000 2.0667 3.0000
现在我想得到这个结果,但是对于 10 个点,全部存储在 [200x3] 数组中,间隔为 20 帧。
有什么想法吗?
提前致谢!
如果您有图像处理工具箱 blockproc
可能是一个选项:
A = blockproc(data,[20 3],@(x) mean(x.data,1))
如果不是,则以下使用 permute
with reshape
也有效:
B = permute(mean(reshape(data,20,10,3),1),[2,3,1])
解释:
%// transform data to 3D-Matrix
a = reshape(data,20,10,3);
%// avarage in first dimension
b = mean(a,1);
%// transform back to 10x3 matrix
c = permute(b,[2,3,1])
一些示例数据:
x = [ 1.0000 2.0000 3.0000
1.1000 2.2000 2.9000
0.9000 2.0000 3.1000
1.0000 2.0000 3.0000
1.1000 2.2000 2.9000
0.9000 2.0000 3.1000
1.0000 2.0000 3.0000
1.1000 2.2000 2.9000
0.9000 2.0000 3.1000
1.1000 2.2000 2.9000]
data = kron(1:20,x.').';
A = B =
1.5150 3.1200 4.4850
3.5350 7.2800 10.4650
5.5550 11.4400 16.4450
7.5750 15.6000 22.4250
9.5950 19.7600 28.4050
11.6150 23.9200 34.3850
13.6350 28.0800 40.3650
15.6550 32.2400 46.3450
17.6750 36.4000 52.3250
19.6950 40.5600 58.3050
如果您无法访问 blockproc
函数,您可以使用 reshape
:
的组合来实现
np = 20 ; %// number of points for averaging
tmp = reshape( A(:) , np,[] ) ; %// unfold A then group by "np"
tmp = mean(tmp); %// calculate mean for each group
B = reshape(tmp, [],3 ) ; %// reshape back to nx3 matrix
在您的情况下,将 A
替换为 MeasuredFrames
,将 B
替换为 Points
,并在一行中分组:
Points = reshape(mean(reshape( MeasuredFrames (:) , np,[] )), [],3 ) ;
可以使用矩阵乘法:
N=20;
L=size(MeasuredFrames,1);
Points = sparse(ceil((1:L)/N), 1:L, 1)*MeasuredFrames/N;
我有一个相当简单的问题,但我无法在 MATLAB 中得到正确的结果。
我正在 Matlab 中编写代码,其中有一个 200x3 矩阵。这个数据对应10个不同点的记录,我每个点都拍了20帧。
这只是为了解决测量系统中的误差。所以现在我想通过计算测量的独立坐标的平均值来计算这个矩阵中每个点的 3D 坐标。
一个例子(1 个点有 3 个测量值)是:
MeasuredFrames (Point 1) =
x y z
1.0000 2.0000 3.0000
1.1000 2.2000 2.9000
0.9000 2.0000 3.1000
Point = mean(MeasuredFrames(1:3, :))
Point =
1.0000 2.0667 3.0000
现在我想得到这个结果,但是对于 10 个点,全部存储在 [200x3] 数组中,间隔为 20 帧。
有什么想法吗?
提前致谢!
如果您有图像处理工具箱 blockproc
可能是一个选项:
A = blockproc(data,[20 3],@(x) mean(x.data,1))
如果不是,则以下使用 permute
with reshape
也有效:
B = permute(mean(reshape(data,20,10,3),1),[2,3,1])
解释:
%// transform data to 3D-Matrix
a = reshape(data,20,10,3);
%// avarage in first dimension
b = mean(a,1);
%// transform back to 10x3 matrix
c = permute(b,[2,3,1])
一些示例数据:
x = [ 1.0000 2.0000 3.0000
1.1000 2.2000 2.9000
0.9000 2.0000 3.1000
1.0000 2.0000 3.0000
1.1000 2.2000 2.9000
0.9000 2.0000 3.1000
1.0000 2.0000 3.0000
1.1000 2.2000 2.9000
0.9000 2.0000 3.1000
1.1000 2.2000 2.9000]
data = kron(1:20,x.').';
A = B =
1.5150 3.1200 4.4850
3.5350 7.2800 10.4650
5.5550 11.4400 16.4450
7.5750 15.6000 22.4250
9.5950 19.7600 28.4050
11.6150 23.9200 34.3850
13.6350 28.0800 40.3650
15.6550 32.2400 46.3450
17.6750 36.4000 52.3250
19.6950 40.5600 58.3050
如果您无法访问 blockproc
函数,您可以使用 reshape
:
np = 20 ; %// number of points for averaging
tmp = reshape( A(:) , np,[] ) ; %// unfold A then group by "np"
tmp = mean(tmp); %// calculate mean for each group
B = reshape(tmp, [],3 ) ; %// reshape back to nx3 matrix
在您的情况下,将 A
替换为 MeasuredFrames
,将 B
替换为 Points
,并在一行中分组:
Points = reshape(mean(reshape( MeasuredFrames (:) , np,[] )), [],3 ) ;
可以使用矩阵乘法:
N=20;
L=size(MeasuredFrames,1);
Points = sparse(ceil((1:L)/N), 1:L, 1)*MeasuredFrames/N;