i need to compare s=[0 1] with I=[0 0;1 1;1 0;0 1;1 0;0 0; 1 0; 1 1; 0 0;.............................90bits] i want to use counter .can any one help me

 Respuesta aceptada

Paulo Silva
Paulo Silva el 24 de Jun. de 2011

1 voto

s=[0 1];
I=[0 0;1 1;1 0;0 1;1 0;0 0; 1 0; 1 1; 0 0]
counter=0;
for n=1:size(I,1)
if I(n,:)==s
counter=counter+1;
else
%nothing to be done, just included the else for fun :)
end
end
counter
Alternative just for fun
s=[0 1];
I=[0 0;1 1;1 0;0 1;1 0;0 0; 1 0; 1 1; 0 0]
counter=0;
for n=1:size(I,1)
if I(n,:)~=s
%nothing to be done here :)
else
counter=counter+1;
end
end
counter

2 comentarios

mahaveer hanuman
mahaveer hanuman el 24 de Jun. de 2011
i am getting answer as 0.
Sean de Wolski
Sean de Wolski el 24 de Jun. de 2011
sum(all(bsxfun(@eq,s,I),2))
also for fun!

Iniciar sesión para comentar.

Más respuestas (1)

Sean de Wolski
Sean de Wolski el 24 de Jun. de 2011

0 votos

idx = ismember(I,s,'rows'); %row indices of matches
nmatches = sum(idx); %number of matches
row_indices = find(idx); %row numbers of matches
A few of the things you can do...

3 comentarios

mahaveer hanuman
mahaveer hanuman el 24 de Jun. de 2011
acutaly i need to use counter so can u help using counter in "if else "
Sean de Wolski
Sean de Wolski el 24 de Jun. de 2011
Why? nmatches will be the same result as a counted (like in Paulo's below example)
I guess this must be homework...
Paulo Silva
Paulo Silva el 24 de Jun. de 2011
I was almost commenting the same you did :) your code is the best way to do it unless it's really homework.

Iniciar sesión para comentar.

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by