例如,
A = [1 0 1 1 0 -1 0 1 -1]
for i = 1: length(A)
sum(A(1:i)) ~= 1 && sum(A(1:i)) ~= 0
时如何将数字标记为“0”?
以及如何编写代码?
答案 0 :(得分:3)
A = [1 0 1 1 0 -1 0 1 -1]
%# create all sums from 1 through i
sa = cumsum(A);
%# in output: ith element is true if sum from
%# 1 through the ith element in A is 0 or 1
output = ismember(sa,[0 1])
output =
1 1 0 0 0 0 0 0 0