user3263112
user3263112

Reputation: 57

Martix Logical comparison

In matlab i have hue plane in a matrix and the most common colors(top 5% of the hues) in an another matrix(L). I want to create a binary image where only rare colors are present.

Hue plane is 253X320 matrix the L matrix is 6X1.

for l = 1 : size(HuePlane,1) for m = 1 : size(HuePlane,2) for n = 1:size(L,1) if L(n,1) == HuePlane(l,m) H(l,m) = 0; else
H(l,m) = 1; end end end end

This results in a matrix of only 1s.

Upvotes: 0

Views: 43

Answers (2)

Yvon
Yvon

Reputation: 2993

(I'm trying to follow your original idea)

h = 0*H;
for ii = 1:length(L)
    hh = H==L(ii);
    h = max(hh,h);
end

Upvotes: 0

Dan
Dan

Reputation: 45752

As Daniel mentioned, using ismember is the best solution and you should use it:

H = ~ismember(HuePlane, L)

However I thought I'd show you where you've gone wrong in your loop solution. Basically you're always comparing each colour in HuePlane to EVERY element of L sequentially which means you only store the result of the LAST comparison. In otherwords you are only checking against L(end). This is what I think you were trying to do:

H = ones(size(HuePlane)); %// This pre-allocation is essential in Matlab! Your code will be terribly inefficient if you don't do it.
for l = 1 : size(HuePlane,1)
    for m = 1 : size(HuePlane,2)
        for n = 1:size(L,1)
            if L(n,1) == HuePlane(l,m)
                H(l,m) = 0;
                break; %// No point checking against the rest of L once we've found a match!
            end
        end
    end
end

But this is a very inefficient way to go.

Upvotes: 1

Related Questions