我有一个256X192X80
的矩阵。我想在不使用for
循环的情况下规范化所有切片(80表示切片)。
我正在使用for
的方式如下:( im_dom_raw是我们的矩阵)
normalized_raw = zeros(size(im_dom_raw));
for a=1:80
slice_raw = im_dom_raw(:,:,a);
slice_raw = slice_raw-min(slice_raw(:));
slice_raw = slice_raw/(max(slice_raw(:)));
normalized_raw(:,:,a) = slice_raw;
end
答案 0 :(得分:3)
下面的代码在不使用循环的情况下实现了规范化方法。它基于bsxfun
。
% Shift all values to the positive side
slices_raw = bsxfun(@minus,im_dom_raw,min(min(im_dom_raw)));
% Normalize all values with respect to the slice maximum (With input from @Daniel)
normalized_raw2 = bsxfun(@mrdivide,slices_raw,max(max(slices_raw)));
% A slightly faster approach would be
%normalized_raw2 = bsxfun(@times,slices_raw,max(max(slices_raw)).^-1);
% ... but it will differ with your approach due to numerical approximation
% Comparison to your previous loop based implementation
sum(abs(normalized_raw(:)-normalized_raw2(:)))
最后一行代码输出
ans =
0
其中(感谢@Daniel)意味着两种方法都会产生完全相同的结果。