Sum of the element occurence

조회 수: 1 (최근 30일)
yiwen yim
yiwen yim 2018년 4월 29일
댓글: Ameer Hamza 2018년 4월 29일
Greetings Matlab Community,
I've manage to sum up the occurrence of number 2 of the entire matrix by using this,
chromo = [1 1 2 4 6 1 ; 2 1 3 5 7 2];
sum(chromo(:) == 2)
How do i sum up the occurrence of number 2 in just a row in the matrix and not the entire matrix?

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 4월 29일
If A is the matrix, you can do this as follow
numOfTwos = sum(A==2, 2);
myResult = numOfTwos(rowNumber);
The above will numOfTwos will give number fo two in all the rows of matrix A. If you just want it for one row, use
numOfTwos = sum(A(rowNumber, :) ==2, 2);
  댓글 수: 7
Ameer Hamza
Ameer Hamza 2018년 4월 29일
@yiwen yim answer moved here
"Also for a particular row."
Ameer Hamza
Ameer Hamza 2018년 4월 29일
For particular row you can do this
numOfTwos = sum(A(3, :) ==2, 2);
It will tell you number of twos in row 3.

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

추가 답변 (2개)

yiwen yim
yiwen yim 2018년 4월 29일
I would like to know how to do it for each row.
  댓글 수: 1
Ameer Hamza
Ameer Hamza 2018년 4월 29일
Please delete this answer, and discuss it in the comment section.

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


yiwen yim
yiwen yim 2018년 4월 29일
Also for a particular row.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by