if else
조회 수: 16 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
추가 답변 (1개)
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
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
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!