MATLAB: How do I find a number that matches a certain condition and mark it as "0"?

For example,

A = [1 0 1 1 0 -1 0 1 -1]

for i = 1: length(A)

      

how to mark number as "0" when sum(A(1:i)) ~= 1 && sum(A(1:i)) ~= 0

?

and how to write the code?

+3


source to share


1 answer


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

      



+3


source







All Articles