Matlab bsxfun 替代方法导致错误结果
Matlab bsxfun alternative cause wrong result
我必须 运行 在不支持 bsxfun
的旧版本上编写 Matlab 代码,并且需要编写
的等效表达式
matx = bsxfun(@rdivide, matx, reshape(f, 1, 1, length(f)));
我试过了
matx=matx./ones(size(reshape(f, 1, 1, length(f)),1));
但我得到错误的结果
matx
尺寸为 246x301x81
f
在调用使用 bsxfun 的指令之前大小为 1x81
由于 matx
是一个 3D 数组,而 f
是一个行向量,其长度等于 matx
的 dim-3
中的元素数,您可以执行bsxfun
等价于 expansion/replication 和 repmat
然后像这样执行元素除法 -
% Get size of matx
[m1,n1,r1] = size(matx);
%// Replicate f to the size of matx and perform elementwise division
matx = matx./repmat(permute(f,[1 3 2]),[m1 n1 1])
我必须 运行 在不支持 bsxfun
的旧版本上编写 Matlab 代码,并且需要编写
matx = bsxfun(@rdivide, matx, reshape(f, 1, 1, length(f)));
我试过了
matx=matx./ones(size(reshape(f, 1, 1, length(f)),1));
但我得到错误的结果
matx
尺寸为 246x301x81
f
在调用使用 bsxfun 的指令之前大小为 1x81
由于 matx
是一个 3D 数组,而 f
是一个行向量,其长度等于 matx
的 dim-3
中的元素数,您可以执行bsxfun
等价于 expansion/replication 和 repmat
然后像这样执行元素除法 -
% Get size of matx
[m1,n1,r1] = size(matx);
%// Replicate f to the size of matx and perform elementwise division
matx = matx./repmat(permute(f,[1 3 2]),[m1 n1 1])