I have a 500x500 sparse matrix with values ranging from 0 to a.
I want to change its elements according to a list working as a dictionary. Elements that equal 1 become the value of list(1), that equal 2 become the value of list(2), etc until values that equal a become list(a)
Is there a simple way to do this?
CodePudding user response:
Assuming your sparse matrix contains only integers between 0 and a, here is one way using logical indexing:
S = your sparse matrix
L = logical(S); % mask of elements to replace
S(L) = list(S(L)); % replace mask elements with corresponding list elements
CodePudding user response:
This can be done by at least three methods:
method1
[r, c, v] = find(m); m = sparse(r, c, list(v));
method2
m(find(m)) = list(nonzeros(m));
method3
m = spfun(@(x)list(x), m);