if statement - MATLAB - How to create output for each iteration of a looped calculation -
this calculation aims take value (a
) , perform 1 of 2 calculations on each iteration of loop. each sequential loop takes new calculated value , recalculates it. keep outputted value each loop. here example, using logical array decide calculation use on each of 5 loops:
a = 0; b = logical([1 0 1 1 0]); = 1:length(b) if b(i) == 1 = 1*2 + a*2 else = a*3 end end
this example not correct returns 1 value a
rather 5. desired result example is:
a = 2 6 14 30 90
please me , amend else wrong here. many thanks.
you can do
a = [0]; b = logical([1 0 1 1 0]); = 1:length(b) if b(i) == 1 a(end+1) = 1*2 + a(end)*2; else a(end+1) = a(end)*3; end end
Comments
Post a Comment