how to creat this transformation?

1 view (last 30 days)
benghenia aek
benghenia aek on 2 Feb 2019
Answered: madhan ravi on 2 Feb 2019
Hi every one, I have a vector A. All values of vector A are either zeros or 1 numbers
A=[0 0 1 1 1 1 0 0 0 1 1 1 1 1 1 1 0]
How to convert half of each segment of 1 in to 0 in order to obtein vector B
B=[0 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0]

Answers (1)

madhan ravi
madhan ravi on 2 Feb 2019
B=A;
index=find(A);
idx=find(diff(index)~=1);
b=[idx(1) diff(idx) numel(index)-idx(end)];
R=mat2cell(A(index),1,b);
C=cellfun(@(x)[ones(1,fix(numel(x)/2)) ...
zeros(1,numel(x)-fix(numel(x)/2))],R,'un',0);
z=mat2cell(index,1,b);
B([z{:}])=[C{:}]

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!