automatically create a group of same elements from an array

조회 수: 7 (최근 30일)
Surya Putra Kartolo
Surya Putra Kartolo 2018년 8월 29일
답변: KL 2018년 8월 29일
hello, i have a question about how to automatically group same elements of an array. Example: A=(3,3,3,2,1,4,5,5)
i want to group it into: Group1=(1) Group2=(2) Group3=(3,3,3) Group4=(4) Group5=(5,5)
but sometimes, the elements can be varied and it depends on the calculation. So the number of groups cannot be determined manually. So may i ask your idea, how to make the group automatically based on A ,using for loop?
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2018년 8월 29일
Surya - please clarify what you mean by the elements can be varied and it depends on the calculation. Also, are you elements always integers?
Surya Putra Kartolo
Surya Putra Kartolo 2018년 8월 29일
yeah, its always integers. i mean is that the integers in A can change depends on the calculation of the image. so sometimes A=(3,3,3,2,1,4,5,5), and if i do for other calculation A can be like A=(6,6,4,4,3,1,2,2). so the integers can be varied.

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

답변 (1개)

KL
KL 2018년 8월 29일
try this,
result =mat2cell(sort(A),1, histcounts(findgroups(A))) %A is your array
Output is a cell array where you have groups separated into columns.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by