Search code examples
imagematlabpixelbelongs-to

Checking if pixel belongs to an image


I have written the following function that find if a pixel belongs to an image in matlab.

At the beginning, I wanted to test it as if a number in a set belongs to a vector like the following:

function traverse_pixels(img)
for i:1:length(img)
    c(i) = img(i)
end

But, when I run the following commands for example, I get the error shown at the end:

>> A = [ 34 565 456 535 34 54 5 5 4532 434 2345 234 32332434];
>> traverse_pixels(A);
??? Error: File: traverse_pixels.m Line: 2 Column: 6
Unexpected MATLAB operator.

Why is that? How can I fix the problem?

Thanks.


Solution

  • There is a syntax error in the head of your for loop, it's supposed to be:

    for i = 1:length(img)
    

    Also, to check if an array contains a specific value you could use:

    A = [1 2 3]
    if sum(A==2)>0
        disp('there is at least one 2 in A')
    end
    

    This should be faster since no for loop is included.