Elegant vectorized version of CHANGEM (substitute values) - MATLAB

arrays, matlab, matrix, vectorization

Solution

Yes, use `ismember`:

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 :)

Problem

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?

Original source