Borrar filtros
Borrar filtros

Count repetitions separately in an array

2 visualizaciones (últimos 30 días)
Lea Martine Francoise
Lea Martine Francoise el 17 de Mzo. de 2023
Comentada: Lea Martine Francoise el 17 de Mzo. de 2023
I have this array
A = [1;1;2;2;2;2;2;2;3;1;1;1;1;4;4;4;1;1;1;5;5];
I want Matlab to tell me when the repetition of 1 is greater than 2 for example, but I don't know any function that does this.
Could you help ? Thanks a lot !
  1 comentario
Lea Martine Francoise
Lea Martine Francoise el 17 de Mzo. de 2023
For example, I want Matlab to tell me the position in my vector of where I have 4 ones in a row for example which here would be indices 10 to 14

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 17 de Mzo. de 2023
Editada: Stephen23 el 17 de Mzo. de 2023
A = [1;1;2;2;2;2;2;2;3;1;1;1;1;4;4;4;1;1;1;5;5];
D = diff([0;A;0]==1);
B = find(D>0);
E = find(D<0);
L = E-B
L = 3×1
2 4 3
X = find(L>2);
B(X) % start indices
ans = 2×1
10 17
E(X)-1 % end indices
ans = 2×1
13 19
  2 comentarios
Lea Martine Francoise
Lea Martine Francoise el 17 de Mzo. de 2023
I think that would be perfect but how do I get the end indices also ? Because I want to fill those indices with NaN. For example for a condition when I have 6 ones in a row, I want to replace them by NaN
Lea Martine Francoise
Lea Martine Francoise el 17 de Mzo. de 2023
Thank you very much !!

Iniciar sesión para comentar.

Más respuestas (1)

Antoni Garcia-Herreros
Antoni Garcia-Herreros el 17 de Mzo. de 2023
Hello Lea,
Try something like this:
% Where 1 would be the number you are looking and 2 the number of repeats
sum(A==1)>2 % True if there are more than two instances of 1 in your array
  1 comentario
Lea Martine Francoise
Lea Martine Francoise el 17 de Mzo. de 2023
Yes but I guess this does not give me the indices of where I have for example 5 ones in a row and this is what I am looking for :)

Iniciar sesión para comentar.

Categorías

Más información sobre Logical 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