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

2 ビュー (過去 30 日間)
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

採用された回答

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGPU Computing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by