How to compare a matrix of values to all values within an array without loops

1 view (last 30 days)
I'm looking to compare a gray-scale image to an array of values and if a value does not exist within the image, increment a seperate matrix as a counter. I'd like to do this without for loops because it can take several of seconds per frame.
As an example, if i have the following fake image:
A = [1 2 4; 5 6 9;12 13 15];
B then is an array defined by min(A(:)):max(A(:)) to cover all possible values.
The following code does what i need and the output is the following:
grayLevelZeros = setdiff(B,unique(A))-1;
grayLevelZerosFix = zeros(size(A));
for i = 1:length(grayLevelZeros)
grayLevelZerosFix(A>grayLevelZeros(i)) = i;
end
grayLevelZerosFix =
0 0 1
1 1 3
5 5 6
How could I do this without using a for loop to speed up the processing?
  1 Comment
Stephen23
Stephen23 on 22 Mar 2023
"How could I do this without using a for loop to speed up the processing?"
Did you PROFILE the code? SETDIFF is likely the slowest part of that code, not the loop.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 22 Mar 2023
Edited: Matt J on 22 Mar 2023
A = [1 2 4; 5 6 9;12 13 15];
grayLevelZeros = setdiff(min(A(:)):max(A(:)),unique(A))-1;
grayLevelZerosFix = discretize(A,[-inf,grayLevelZeros,inf],'inc','right')-1
grayLevelZerosFix = 3×3
0 0 1 1 1 3 5 5 6

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!