一つの行をグループ分けすることはできますか?
古いコメントを表示
ある1×1行のlogical配列,例えば
000111110011110001111
を0,1で振り分けて以下のように1×6セル配列などにグループ分けすることはできますか?
{000}{11111}{00}{1111}{000}{1111}
採用された回答
その他の回答 (1 件)
Kojiro Saito
2021 年 5 月 12 日
きれいなやり方ではなく、愚直に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
カテゴリ
ヘルプ センター および File Exchange で 行列および配列 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!