Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Accepted Answer

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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!