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 CenterFile Exchange에서 GPU Computing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by