一つの行をグループ分けすることはできますか?
Show older comments
ある1×1行のlogical配列,例えば
000111110011110001111
を0,1で振り分けて以下のように1×6セル配列などにグループ分けすることはできますか?
{000}{11111}{00}{1111}{000}{1111}
Accepted Answer
More Answers (1)
Kojiro Saito
on 12 May 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
Categories
Find more on 行列および配列 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!