필터 지우기
필터 지우기

Summation of values > 1 in a cell array

조회 수: 1 (최근 30일)
RDG
RDG 2013년 4월 12일
Suppose I've a cell array;
cnt{1}=[3;1;3;2;1;2]
I would like to sum up the values which are > 1 ie the answer should return 10 (3+3+2+2)
I've tried using 'find' but the answer only returns the indices of the values > 1 and not the values.
ind=(find(cnt{1}>1))
How can I go about this? Please give me some hint.

채택된 답변

Ilham Hardy
Ilham Hardy 2013년 4월 12일
sumVal = sum(cnt{1})-length(find(cnt{1}==1))
  댓글 수: 3
Ilham Hardy
Ilham Hardy 2013년 4월 12일
Yes, i agree. This way will be faster.
However i don't fully get the differences between example#1 and example#2. Care to explain?
Matt J
Matt J 2013년 4월 12일
The 2nd method would be needed if the threshold were something other than 1.

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

추가 답변 (1개)

Matt J
Matt J 2013년 4월 12일
편집: Matt J 2013년 4월 12일
ind=(cnt{1}>1);
sum(cnt{1}(ind)),

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by