grouping data based on the times a value is repeated

조회 수: 3 (최근 30일)
Georgios Tertikas
Georgios Tertikas 2018년 2월 28일
댓글: Guillaume 2018년 3월 5일
I have data in array form with big number of rows and 12 columns. One of the column has an increasing array but not in a stable way (e.g. 1 1 1 1 1 2 2 2 2 3 3 4 4 4 4 4 4 4 4 5 6 6 6 etc)I want to organize data so I have all rows with 1 repetition of the value, 2 repetitions and so forth without disrupting the order of the sequence between each number. So I want to have an array that in the beginning was like this:
[1 45 67]
[1 23 89]
[1 90 110]
[2 41 52]
[2 51 76]
[2 77 88]
[2 13 14]
[3 545 111]
[3 242 53]
[3 80 23]
[4 11 22]
[4 14 26]
[4 16 77]
[4 11 943]
and I want it to be like that:
[1 45 67]
[1 23 89]
[1 90 110]
[3 545 111]
[3 242 53]
[3 80 23]
and
[2 41 52]
[2 51 76]
[2 77 88]
[2 13 14]
[4 11 22]
[4 14 26]
[4 16 77]
[4 11 943].
The data is all numerical and all rows have values in all columns.
Is there a way to do this with MATLAB?
Thank you very much.

채택된 답변

Guillaume
Guillaume 2018년 2월 28일
m = [1 45 67; 1 23 89; 1 90 110; 2 41 52; 2 51 76; 2 77 88; 2 13 14; 3 545 111; 3 242 53; 3 80 23; 4 11 22; 4 14 26; 4 16 77; 4 11 943]
numreps = diff(find(diff([0;m(:, 1);0]))); %or use any histogram function
[~, neworder] = sort(numreps);
splitm = mat2cell(m, numreps, size(m, 2));
reorderedm = cell2mat(splitm(neworder))
Other ways of obtaining numreps:
numreps = accumarray(m(:, 1), 1); %only if m(:, 1) is integer from 1 to x with no gap.
numreps = histcounts(m(:, 1), 'BinMethod', 'integers'); %only if m(:, 1) is integer from 1 to x with no gap.
numreps = histcounts(m(:, 1), [unique(m(:, 1)); Inf]);
  댓글 수: 3
Guillaume
Guillaume 2018년 3월 5일
If I understand correctly what you're asking, replace the 2nd line with
[sortedreps, neworder] = sort(numreps);
to find the length of each run
runlength = diff(find(diff([0; sortedreps; 0])))
Georgios Tertikas
Georgios Tertikas 2018년 3월 5일
thank you very much

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2018년 2월 28일
Have you tried taking the histogram of the first column?
  댓글 수: 5
Image Analyst
Image Analyst 2018년 3월 5일
편집: Image Analyst 2018년 3월 5일
You must have a really old version of MATLAB. You can use hist() or histc() instead.
Guillaume
Guillaume 2018년 3월 5일
In my answer, under "Other ways of obtaining numreps", I showed two different ways of using histcounts. Because of the automatic binning you can't just pass the column of number.

댓글을 달려면 로그인하십시오.


Georgios Tertikas
Georgios Tertikas 2018년 3월 5일
i meant it works but it doesnt show the results i need

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by