Vectorized implementation of CHANGEM with bsxfun , max
Once CHANGEM a time, I was created to write a custom vector version of CHANGEM implemented with bsxfun and max as part of a much more serious problem. Reference solution can be found here . Then, after several links, I saw this post and thought that it could be published here as a solution for easy finding among future readers, and also because this problem exclusively requires an effective and vectorized version of CHANGEM . So here the function code is -
%// CHANGEM_VECTORIZED Vectorized version of CHANGEM with MAX, BSXFUN function B = changem_vectorized(A,newval,oldval) B = A; [valid,id] = max(bsxfun(@eq,A(:),oldval(:).'),[],2); %//' B(valid) = newval(id(valid)); return;
The syntax used in the user version follows the same syntax as in changem.m -
function B = changem(A, newval, oldval) %CHANGEM Substitute values in data array ...
Divakar
source share