Divide an array in areas based on its values
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 28 Mai 2013
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
0 Kommentare
Weitere Antworten (2)
Andrei Bobrov
am 28 Mai 2013
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});
0 Kommentare
Giorgos Papakonstantinou
am 28 Mai 2013
3 Kommentare
Andrei Bobrov
am 29 Mai 2013
Please read the description of the three functions in the MATLAB documentation: diff, cumsum, accumarray.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!