How to compare a matrix of values to all values within an array without loops
1 view (last 30 days)
Show older comments
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
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.
Accepted Answer
More Answers (0)
See Also
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!