need function to compute iterations of element in matrix

4 views (last 30 days)
hi every body i need matlab function to compute the number of iterations of one elements in matrix just like in excel when u want to compute for example how much the No. 3 occur in some column = counif(a1:a10;"=3") { in excel } is there a function in matlab can do the same thinks
tanks in advance

Accepted Answer

AJ von Alt
AJ von Alt on 28 Jan 2014
One approach is to create a logical matrix for the input by comparing it to the target value. You can then sum a row, column, or the whole matrix to get the number of instances, since each instance is indicated by a 1.
% create a 50 x 50 matrix taking integers between 1 and 10
M = randi( 10 , 50 , 50 );
% the value you wish to count instances of
targetVal = 3;
% create logical matrix indicating presence of targetVal at location
isTarget = (M == targetVal);
% Find the number of instances in the matrix
nVals = sum( isTarget(:) );
% Find the number of instances in row 3
nvalsR3 = sum( isTarget(3,:) );
% Find the number of instances in column 12
nvalsC12 = sum( isTarget(:,12) );

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 28 Jan 2014
Edited: Azzi Abdelmalek on 28 Jan 2014
A=[1 2 3;3 0 1;4 5 6;3 1 5]
n1=sum(A(:,1)==3) % number of 3 in the first column
n2=sum(A(:,3)==3) % number of 3 in the third column


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!