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 件のコメント

Image Analyst
Image Analyst 2013 年 5 月 28 日
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 5 月 28 日
Obviously its duplicated. I did because the comments are sometimes neglected. :-)

サインインしてコメントする。

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 28 日

0 投票

a=[1 1 1 1 1 1 12 10 1 1 1 1 1 11 12 1 1 1 2 3 ]
idx=find(a==1)
e=[diff(idx) 100]
f=find(e~=1)
ff=idx(f)
ii1=[idx(1) idx(f(1:end-1)+1)]
ii2=idx(f)
idx=find(a~=1)
e=[diff(idx) 100]
f=find(e~=1)
ff=idx(f)
jj1=[idx(1) idx(f(1:end-1)+1)]
jj2=idx(f)
for k=1:numel(ii1)
area1(k)={ii1(k):ii2(k)};
area0(k)={jj1(k):jj2(k)};
end

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2013 年 5 月 28 日

0 投票

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
Giorgos Papakonstantinou 2013 年 5 月 28 日

0 投票

I amazed by the simplicity of the codes both of you provided. Although I have some difficulty to understand them they solve my problem.
I would be grateful if yo provided some explanation Thank you very much.

3 件のコメント

Image Analyst
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
Giorgos Papakonstantinou 2013 年 5 月 28 日
This answer is common for both of the answers that I got above considering the level of my knowledge in matlab. Therefore this answer corresponds to Azzi's and Andrei's answer and this the reason that I posted as an answer and not as a comment. I would accept both answers if this was possible. I accepted Azzi's because it is easier to comprehend.
Andrei Bobrov
Andrei Bobrov 2013 年 5 月 29 日
Please read the description of the three functions in the MATLAB documentation: diff, cumsum, accumarray.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by