MATLAB:如何找到满足一定条件的数字并将其标记为“0”?

时间:2012-03-26 13:15:41

标签: matlab

例如,

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”?

以及如何编写代码?

1 个答案:

答案 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