필터 지우기
필터 지우기

Split array into groups of close numbers

조회 수: 9 (최근 30일)
Denis Perotto
Denis Perotto 2023년 12월 8일
답변: Stephen23 2023년 12월 8일
Greetings,
Is there a built-in function in MATLAB to split an 1D array into groups of close numbers?
For example, I have an array
a = [1 2 3 10 11]
and want to split it into N = 2 groups. I expect to see something like
result{1} = [1 2 3]
result{2} = [10 11]
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 12월 8일
How do you define close numbers?
Where the difference between adjacent numbers in 1?

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

채택된 답변

Stephen23
Stephen23 2023년 12월 8일
a = [1,2,3,10,11];
n = 2;
x = kmeans(a(:),n)
x = 5×1
2 2 2 1 1
c = accumarray(x,a(:),[],@(a){a})
c = 2×1 cell array
{2×1 double} {3×1 double}

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 12월 8일
No -- because there is no rigourous meaning to what "close" means.
For example, in [1 3 5 7], surely 1 is not "close" to 7, but 5 is (probably) close to 7. Then 3 is "close" to 5, and 1 is "close" to 3. So are they all to be considered close as a group, even though 1 is "far" from 7 ?
Now, if the question were about dividing up into groups of consecutive values, then that would not be too difficult.
You could also use something like
N = 2;
a = [1 2 3 10 11]
a = 1×5
1 2 3 10 11
[g, centroids] = kmeans(a(:), N)
g = 5×1
1 1 1 2 2
centroids = 2×1
2.0000 10.5000

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by