Need to counter number of repeats in a vector and store in a matrix with repeated value and amount of times repeated.

1 visualización (últimos 30 días)
Hi all,
So my problem is this: I have a vector of failure times. I need to go through this vector and find all the repeating times and count each time it's repeated and then write a matrix or two vectors of the same length with the failure time and how many times it occurred.
So, for example, let's say my failure times, x, looked like: x = [20;30;40;40;50;50;50;60;70]; what I want is to get something like this: y = [20 1; 30 1; 40 2; 50 3; 60 1; 70 1]; OR y = [20;30;40;50;60;70]; z = [1;1;2;3;1;1];
so that I can then proceed with failure forecasting and finding the expected failures now. Since I'm not sure what the data would look like, I'm not sure how to make an empty vector/matrix to hold the values unless I did it based on finding the amount of repeats:
repeatedVals = length(unique(x) < length(x));
which returns the amount of repeated values and then I could do an empty vector/matrix the length of N (total failure times) - repeatedVals...
Anyone have any ideas?
Thanks! E

Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Jul. de 2017
x = [20;30;40;40;50;50;50;60;70];
[y, ~, uidx] = unique(x);
z = accumarray(uidx, 1);

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices 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