Divide an array in areas based on its values
이전 댓글 표시
Suppose that I want find the edges of ones and of not ones such that I can divide an in areas the array. Suppose the array is as the example below:
a=[1 1 1 1 1 1 12 10 1 1 1 1 1 11 12 1 1 1 2 3]
a(1:6) -->area1 of ones
a(7:8) -->area1 of not ones
a(9:a13) -->area2 of ones
a(14:15)-->area2 of not ones
a(16:19)-->area3 of ones
a(20) -->area3 of not ones
and so on..
채택된 답변
추가 답변 (2개)
Andrei Bobrov
2013년 5월 28일
a=[1 1 1 1 1 1 12 10 1 1 1 1 1 11 12 1 1 1 2 3 ];
a1 = a == 1;
ii = [true, diff(a1)~=0];
idx = cumsum(ii);
out = accumarray(idx(:),a(:),[],@(x){x});
Giorgos Papakonstantinou
2013년 5월 28일
0 개 추천
댓글 수: 3
Image Analyst
2013년 5월 28일
편집: Image Analyst
2013년 5월 28일
Please transfer this "Answer" to the other "Answer" (the one you thought you were responding to) as a comment. No one knows if you're talking to Azzi, whose answer you accepted, or Andrei, who has the shorter answer. Though I do agree with you that any compact, cryptic code could be helped with explanation/comments.
Giorgos Papakonstantinou
2013년 5월 28일
Andrei Bobrov
2013년 5월 29일
Please read the description of the three functions in the MATLAB documentation: diff, cumsum, accumarray.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!