How to count no of iterations of first digit.

1 visualización (últimos 30 días)
Vishal Sharma
Vishal Sharma el 23 de En. de 2017
Respondida: the cyclist el 23 de En. de 2017
I want to count number of times first digit of first column is repeating. If
a = [11 3;
12 4;
13 3;
21 2;
22 5;
23 6;
24 3];
So, I want to get result in form of 1 = 3 and 2 = 4
  2 comentarios
the cyclist
the cyclist el 23 de En. de 2017
Is that column guaranteed to be two-digit numbers, or could it be
a = [ 11 3;
121 4]
?
Vishal Sharma
Vishal Sharma el 23 de En. de 2017
Two digits only

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 23 de En. de 2017
Editada: Stephen23 el 23 de En. de 2017
Assuming that there are always two digits:
>> a = [11 3;
12 4;
13 3;
21 2;
22 5;
23 6;
24 3]
>> tmp = fix(a(:,1)/10);
>> uni = unique(tmp);
>> cnt = histc(tmp,uni)
cnt =
3
4
>> [uni,cnt]
ans =
1 3
2 4

Más respuestas (1)

the cyclist
the cyclist el 23 de En. de 2017
a = [11 3;
12 4;
13 3;
21 2;
22 5;
23 6;
243 3];
a1 = a(:,1);
firstDigits = floor(a1./(10.^floor(log10(a1))));
uniqueFirstDigit = unique(firstDigits);
firstDigitCount = histcounts(firstDigits,[uniqueFirstDigit; Inf])'

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by