連続したある数値以上​の要素を一つのグルー​プとして表したい

A=[-210 -210 -33 -34 -33 -35 -30 -33 -210 -33 -33 -210 -210 -34 -36 -35 -210]
B=[6 2 3]
上のような1行n列のAがあったとして、-210より大きく連続しているもの(左から「-33 -34 -33 -35 -30 -33 」、「-33 -33」、「-34 -36 -35」)のそれぞれの要素数をBのように示したいです。
ご助言よろしくお願いします。

Réponses (2)

Hernia Baby
Hernia Baby le 6 Juin 2022

1 vote

bwlabel 関数を使ってみてください
A=[-210 -210 -33 -34 -33 -35 -30 -33 -210 -33 -33 -210 -210 -34 -36 -35 -210];
[groups, numGroups] = bwlabel(A > -210)
groups = 1×17
0 0 1 1 1 1 1 1 0 2 2 0 0 3 3 3 0
numGroups = 3
for ii = 1:numGroups
B(1,ii) = sum(groups == ii);
end
B
B = 1×3
6 2 3
Atsushi Ueno
Atsushi Ueno le 6 Juin 2022

1 vote

A = [-210 -210 -33 -34 -33 -35 -30 -33 -210 -33 -33 -210 -210 -34 -36 -35 -210];
temp = cumsum(A > -210)
temp = 1×17
0 0 1 2 3 4 5 6 6 7 8 8 8 9 10 11 11
B = diff([0 temp(diff(A > -210) < 0)]) % -210より大きく連続している要素数
B = 1×3
6 2 3

Catégories

En savoir plus sur MATLAB 入門 dans Centre d'aide et File Exchange

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!