Elegant vectorized version of CHANGEM (substitute values) - MATLAB
Date: 2022-09-19 15:43:16
In Matlab 2012b, there is a changem
function that allows you to substitute elements of a matrix with other values specified by a set of keys: Substitute values in data array
Is there an elegant/vectorized way to do the same if I don't have the Mapping toolbox?
Answers:
A = magic(3);
oldCode = [ 8 9];
newCode = [12 13];
[a,b] = ismember(A,oldCode);
A(a) = newCode(b(a));
I don't know changem
, and I suspect the above will not fully cover its functionality (why else would TMW have introduced changem
?), but well, it does what you asked :)