Suppose now I have a matrix
S = [1 1 1 2 2 2;
1 1 1 2 2 2;
2 2 2 2 1 1;
2 2 2 2 1 1;
2 2 2 2 1 1]
And another matrix
A = [1 2;
2 4]
The first row in A is the unique indices of S, and the second row contains the values that the values in the first row will be replaced. That is, all “1”s in S will be replaced by 2, and all “2”s will be replaced by 4. Finally I’ll get a matrix
SS = [2 2 2 4 4 4;
2 2 2 4 4 4;
4 4 4 4 2 2;
4 4 4 4 2 2;
4 4 4 4 2 2]
Right now what I’m doing is:
SS = zeros(size(S));
for i = 1:size(A,2)
SS(S==index(A(1, i)) = A(2,i);
end
Now, I have a pretty big matrix, and using a for loop is a little bit slow. Is there a faster way to do that?
Use the second output of
ismemberto give you indices of the values in row 1 of A. Use these indices to directly create matrixSS.Example (changed initial values for clarity):