一つの行をグループ分けすることはできますか?
Ältere Kommentare anzeigen
ある1×1行のlogical配列,例えば
000111110011110001111
を0,1で振り分けて以下のように1×6セル配列などにグループ分けすることはできますか?
{000}{11111}{00}{1111}{000}{1111}
Akzeptierte Antwort
Weitere Antworten (1)
Kojiro Saito
am 12 Mai 2021
きれいなやり方ではなく、愚直にfor文を使った方法ですが、下記で実現できます。
resultにグループ分けされたセル配列が格納されます。
line = '000111110011110001111';
tmpStr = line(1);
count = 1;
idx = 1;
for n=2:length(line)
% n番目の文字と1つ前の文字を比較し、違ったらresultに文字を格納
if ~strcmp(line(n), tmpStr)
result{count} = line(idx:n-1);
count = count+1;
idx = n;
end
% nが最後の場合はresultに文字を格納
if n == length(line)
result{count} = line(idx:n);
end
% 次のループのために文字を保持
tmpStr = line(n);
end
Kategorien
Mehr zu 行列および配列 finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!