Question

I am almost sure there has to be a MATLAB way for this, but I don't have much experience:

width=length(hsvSecond(:,1,1));
height=length(hsvSecond(1,:,1));
for i=1:width
    for j=1:height
        if(hsvSecond(i,j,2)>0.35)
            hsvSecond(i,j,1)=0;
            hsvSecond(i,j,2)=0;
            hsvSecond(i,j,3)=0;
        end    
    end
end

So, basically, if the hsvSecond(i,j,2) value is bigger than a number, I want to put hsvSecond(i,j,:) to zero.

Was it helpful?

Solution

Here is a simple one-liner:

hsvSecond(repmat(hsvSecond(:,:,2) > 0.35, [1,1,3])) = 0;

or this possibly more efficient alternative (although some rudimentary test suggest otherwise which does surprise me):

hsvSecond = bsxfun(@times, hsvSecond(:,:,2) <= 0.35, hsvSecond)

Some comments on your code though:

width=length(hsvSecond(:,1,1));
height=length(hsvSecond(1,:,1));

Should be

width = size(hsvSecond,1)
height = size(hsvSecond,2)

and

hsvSecond(i,j,1)=0;
hsvSecond(i,j,2)=0;
hsvSecond(i,j,3)=0;

could have been

hsvSecond(i,j,:)=0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top