Matlab bsxfun替代导致错误的结果

时间:2015-05-29 17:33:33

标签: arrays matlab bsxfun

我必须在不支持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 在调用使用bsxfun

的istruction之前,f大小为1x81

1 个答案:

答案 0 :(得分:1)

由于matx是一个3D数组,而f是一个长度等于dim-3 matx中元素数量的行向量,您可以执行{{使用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])