필터 지우기
필터 지우기

comparison/checking without for loop

조회 수: 1 (최근 30일)
Ricky
Ricky 2012년 5월 4일
Hi there,
Say if I have a random event with just -1 and 1. e.g. a = randsrc(5,1);%that will generate a 5x1 matrix with just -1 or 1
Now say if I want to check how many of -1 or 1 in a, how can I do that without using for loop? I try to do this but doesn't work:
m =0;
if(a == 1)
m = m+1;
end
Any help would be really appreciated.
Cheers,
Rak

채택된 답변

Geoff
Geoff 2012년 5월 4일
nneg = sum(a==-1);
npos = sum(a==1);
  댓글 수: 5
Geoff
Geoff 2012년 5월 4일
Yes... replace '0' and '42' in my example with 't1' and 't0' respectively.
Ricky
Ricky 2012년 5월 4일
Thanks Geoff!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by