2

Suppose I have vectors z1 z2 z3 z4 and b and matrices D1 D2 D3 D4.

I want to construct:

b1 = D2*z2 + D3*z3 +D4*z4 -b

b2 = D1*z1 + D3*z3 +D4*z4 -b

b3 = D1*z1 + D2*z2 +D4*z4 -b

b4 = D1*z1 + D2*z2 +D3*z3 -b

I planned to store my z vectors and D matrices in cells and extract them to create b by a for loop. e.g.

for i = 1:3

  b(i) = D{i+1}*z{i+1} + D{i}*z{i};

end

Of course it certainly fails because it involves D{i}*z{i} at each i step. Can you please help me to accomplish my task?

1 Answer 1

2

You can do it like this (no recursion, but still any pair-wise product is only computed once).

pairs = zeros(size(D{1},1), 4);
for ii=4:-1:1,
    pairs(:,ii) = D{ii}*z{ii};
end

Once you have the product of all pairs, you can take the sum

all_sum = sum(pairs, 2) - b_vec; % D1*z1 + D2*z2 + D3*z3 +D4*z4 -b

To get the proper b_i you only need to subtract pairs(:,ii) from the sum:

for ii=4:-1:1
    b{ii} = all_sum - pairs{ii};
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.