need function to compute iterations of element in matrix

7 vues (au cours des 30 derniers jours)
Abdulkareem
Abdulkareem le 28 Jan 2014
Commenté : Abdulkareem le 28 Jan 2014
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

Réponse acceptée

AJ von Alt
AJ von Alt le 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) );
  1 commentaire
Abdulkareem
Abdulkareem le 28 Jan 2014
thank u very much mr. AJ von Alt u answer solve the problem

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 28 Jan 2014
Modifié(e) : Azzi Abdelmalek le 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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by