필터 지우기
필터 지우기

Collapsing integer data into consecutive integers?

조회 수: 3 (최근 30일)
Mark
Mark 2013년 6월 18일
I have a matrix of values that include integers >= 0. There are repeats, and also gaps in consecutiveness. I would like to collapse the data into consecutive integers, keeping the order intact.
Example: [ 0 1 4 4 1 5 8 1 4]
would go to
[ 0 1 2 2 1 3 4 1 2]
I can do this with loops and brute force, but I'm wondering if there's any quick way to do this since it is frequently called within a loop?
Thanks!
  댓글 수: 3
John
John 2013년 6월 18일
It looks like you just divided everything by 2, rounding up.
Mark
Mark 2013년 6월 18일
Not consecutive in the array, but consecutive in the counting of unique numbers in the original. I.e., there are 5 unique numbers in the original vector, 0 1 4 5 and 8. I wanted to map those to 0 1 2 3 4. (The divide by two and round was just coincidence!) The problem has been solved below. Thanks.

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

채택된 답변

Tom
Tom 2013년 6월 18일
[~,~,I] = unique([ 0 1 4 4 1 5 8 1 4]);
disp(I-1)

추가 답변 (1개)

Iain
Iain 2013년 6월 18일
Ex = [ 0 1 4 4 1 5 8 1 4]; % A row
V = unique(Ex); % should be a row
L = bsxfun(@eq,Ex,V);
Collapsed = [0:(numel(V)-1)]*L;
Reconstructed = V' * L;

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by