Sum of the element occurence
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
yiwen yim
am 29 Apr. 2018
Kommentiert: Ameer Hamza
am 29 Apr. 2018
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?
0 Kommentare
Akzeptierte Antwort
Ameer Hamza
am 29 Apr. 2018
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 Kommentare
Ameer Hamza
am 29 Apr. 2018
For particular row you can do this
numOfTwos = sum(A(3, :) ==2, 2);
It will tell you number of twos in row 3.
Weitere Antworten (2)
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!