how to creat this transformation?

3 次查看(过去 30 天)
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]

回答(1 个)

madhan ravi
madhan ravi 2019-2-2
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{:}]

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by