How to pad extra 0 after 5 consecutive 1's in an array

3 ビュー (過去 30 日間)
aamir irshad
aamir irshad 2018 年 6 月 27 日
コメント済み: aamir irshad 2018 年 6 月 27 日
I need to insert extra 0 in a large array after consecutive 5 ones e.g, if
a=[0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 0]..........so on
output array should be
b=[0 1 1 1 1 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0 ]..........so on
Thanks in advance
  2 件のコメント
Paolo
Paolo 2018 年 6 月 27 日
In your input there is a sequence of 6 1s which become 5 1s. Is that also a requirement?
aamir irshad
aamir irshad 2018 年 6 月 27 日
thanks for your reply. Yes, I can explain it a bit more e.g. if we have a= [1 1 1 1 1 1 1 1 1 1 1 1] the output should be b= [1 1 1 1 1 0 1 1 1 1 1 0 1 1] The logic should insert an extra 0 after consecutive 5 ones start looking from the beginning of data.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2018 年 6 月 27 日
Simpler:
>> a = [0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 0];
>> b = regexprep(char(a+'0'),'11111','111110')-'0'
b =
0 1 1 1 1 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0
>> c = [0 1 1 1 1 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0 ] % your requested output
c =
0 1 1 1 1 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0
  1 件のコメント
aamir irshad
aamir irshad 2018 年 6 月 27 日
Hi Stephen,
This is most efficient. Thanks for your help.
BR,
Aamir

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2018 年 6 月 27 日
編集済み: Jan 2018 年 6 月 27 日
a = [1 1 1 1 1 1 1 1 1 1 1 1];
b = a;
idx = strfind(a, [1,1,1,1,1])
if ~isempty(idx)
% Remove indices with a too short distance:
p = idx(1);
for k = 2:numel(idx)
if idx(k) - p < 5
idx(k) = 0;
else
p = idx(k);
end
end
idx = idx(idx ~= 0) + 4;
%
b = cat(1, b, nan(size(b))); % Pad with NaNs
b(2, idx) = 0; % Insert zeros
b = b(~isnan(b)).'; % Crop remaining NaNs
end
This avoids changing the size of the output array in each iteration, which should be more efficient for large data sets.
  1 件のコメント
aamir irshad
aamir irshad 2018 年 6 月 27 日
Its also working perfectly.
Thank you, Jan for another efficient logic.
BR,
Aamir

サインインしてコメントする。


Ameer Hamza
Ameer Hamza 2018 年 6 月 27 日
編集済み: Ameer Hamza 2018 年 6 月 27 日
Try this
a= [1 1 1 1 1 1 1 1 1 1 1 1];
b = a;
count = 1;
while true
index = strfind(b(count:end), [1 1 1 1 1])+5;
if isempty(index)
break
end
index = index(1);
count = count+index-1;
b = [b(1:count-1) 0 b(count:end)];
end
b
b =
Columns 1 through 13
1 1 1 1 1 0 1 1 1 1 1 0 1
Column 14
1
  5 件のコメント
Ameer Hamza
Ameer Hamza 2018 年 6 月 27 日
The code finds the first occurrence of [1 1 1 1 1] in the array and add a zero after that. In the next iteration, it skips the first part of the array and searches the remaining array. If another occurrence of 5 1s is found, it will again add zero and search the remaining array. It will continue until all the groups of 5 1s have passed.
You can set a breakpoint at first line of the code and execute the each line one by one to better understand the logic.
aamir irshad
aamir irshad 2018 年 6 月 27 日
Thanks a lot

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by