Increment different rates in same for-loop
I have 3 different size matrix that are all supposed to be used in the same calculations. The issue I have is that matrix 1 is 8x1, matrix 2 is 4x1 and matrix 3 is 2x1. The increment rates that I need for M1, M2 and M3 are i, j and k respectively.
I need to for instance multiply M1(1,1) with M2(1,1) and M3(1,1) which is easy, but the next step would be
M1(2,1) * M2(1,1) * M3(1,1)
M1(3,1) * M2(2,1) * M3(1,1)
...
M1(8,1) * M2(4,1) * M3(2,1)
So for every iteration of the loop i increments by 1, j increments by half of i and k increments by a quater.
I've attempted to do it like so
for i = 1:8 j = round(i/2); k = round(i/4); M1(i,1) * M2(j,1) * M3(k,1) end
However, k assumes the values of [0 1 1 1 1 2 2 2] which is almost correct. How can I correct my small offset in my k increments - or better yet, is there a smarter way around this problem?
Thanks,
Niclas
1 Comment
Accepted Answer
More Answers (1)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!