if else

9 次查看(过去 30 天)
mahaveer hanuman
mahaveer hanuman 2011-6-24
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

采纳的回答

Paulo Silva
Paulo Silva 2011-6-24
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 个评论
mahaveer hanuman
mahaveer hanuman 2011-6-24
i am getting answer as 0.
Sean de Wolski
Sean de Wolski 2011-6-24
sum(all(bsxfun(@eq,s,I),2))
also for fun!

请先登录,再进行评论。

更多回答(1 个)

Sean de Wolski
Sean de Wolski 2011-6-24
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 个评论
Sean de Wolski
Sean de Wolski 2011-6-24
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 2011-6-24
I was almost commenting the same you did :) your code is the best way to do it unless it's really homework.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by