need function to compute iterations of element in matrix

1 次查看(过去 30 天)
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

采纳的回答

AJ von Alt
AJ von Alt 2014-1-28
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) );

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2014-1-28
编辑:Azzi Abdelmalek 2014-1-28
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

类别

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