matlab - How to code a vectorized expression that depends on other vectorized expression? -
if example have 3 expressions: a, b , c follows:
a(i+1) = a(i) + c(i).k b(i+1) = b(i) + a(i).h c(i+1) = a(i) + b(i) where k , h constants , m , n desired size of c. i previous obtained value, i+1 next value. now, if use for loop, can code as:
a(1)= 2; b(1)= 5; c(1)= 3; i=1:10 a(i+1) = a(i) + c(i)*2; b(i+1) = b(i) + a(i)*3; c(i+1) = a(i) + b(i); end and works fine. want code in vector form, in without having use loop. problem not know how around dependency of:
aon previous value , previouscvaluebon previous values , previouscvalue ofacon previous values ofa,b
here's matrix-based way obtain n-th value of [a;b;c] vector. wouldn't call vectorization, speed things considerably you:
[a,b,c] = deal(zeros(11,1)); a(1)= 2; b(1)= 5; c(1)= 3; %% // original method k=1:10 a(k+1) = a(k) + c(k)*2; b(k+1) = b(k) + a(k)*3; c(k+1) = a(k) + b(k); end %% // matrix method: %// [ ] [1 0 2][ ] %// | b | = |3 1 0|| b | %// [ c ] [1 1 0][ c ] %// i+1 %// %// [ ] [1 0 2][ ] [1 0 2] ( [1 0 2][ ] ) %// | b | = |3 1 0|| b | = |3 1 0| * ( |3 1 0|| b | ) %// [ c ] [1 1 0][ c ] [1 1 0] ( [1 1 0][ c ] ) %// i+2 i+1 %// thus, coefficient matrix taken n-th power, multiplied input %// vector yield values of a(n+1), b(n+1), , c(n+1): m = [1 0 2 3 1 0 1 1 0]; isequal(m^10*[a(1);b(1);c(1)],[a(11);b(11);c(11)]) in reality can use m appropriate power (positive or negative) obtain [a,b,c]n [a,b,c]k ...
Comments
Post a Comment