Borrar filtros
Borrar filtros

need function to compute iterations of element in matrix

1 visualización (últimos 30 días)
Abdulkareem
Abdulkareem el 28 de En. de 2014
Comentada: Abdulkareem el 28 de En. de 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

Respuesta aceptada

AJ von Alt
AJ von Alt el 28 de En. de 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) );

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 28 de En. de 2014
Editada: Azzi Abdelmalek el 28 de En. de 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

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by