MATLAB: Bwconncomp with “labeled” input

arraybwconncomp

I want to pass my segmented image into an algorithm and it requires that the array has all the values in it up till the max value in the array. For example, I will get an error if I pass in the array below because there are no elements with the values [2, 3, 4, 5, 6, 7, 8, 11 to 19]. Is there a function that will reassign the value of each element such that
1 -> 1
9 -> 2
10 -> 3
20 -> 4
It does not have to be ordered. I tried using bwconncomp but it still connects pixels that do not have the same value as long as they are not zero.
BW = logical ([10 10 10 20 9 9 9 9
10 10 10 20 1 1 1 9
10 10 10 20 1 1 1 9
10 10 10 20 20 20 1 9
10 10 10 20 20 20 1 9
10 10 10 20 20 20 9 9]);

Best Answer

You don't need anything like bwconncomp for such a simple operation:
A = [10 10 10 20 9 9 9 9
10 10 10 20 1 1 1 9
10 10 10 20 1 1 1 9
10 10 10 20 20 20 1 9
10 10 10 20 20 20 1 9
10 10 10 20 20 20 9 9]
[~, ~, loc] = unique(A);
reshape(loc, size(A))