Problems with a for loop

1 view (last 30 days)
Antonio Spiere
Antonio Spiere on 16 Oct 2020
Answered: Matt J on 16 Oct 2020
Hello everybody,
I hope, someone can help me. I am desperate about it right now.
I'm trying to implement the following mathematical formula:
tmp = ck * C_1 + ckk * C_2 + ckkk * C_3 + ckkkk * C_4
C_1 to C_4 are fixed values. Now different index combinations are to be tested and these are to be saved. The sum tmp should only occur up to a certain threshold value. A small example:
tmp (1) = 0 * C_1 + 0 * C_2 + 0 * C_3 + 0 * C_4
tmp (2) = 1 * C_1 + 0 * C_2 + 0 * C_3 + 0 * C_4
tmp (3) = 2 * C_1 + 0 * C_2 + 0 * C_3 + 0 * C_4 ...
tmp (xx) = 0 * C_1 + 1 * C_2 + 0 * C_3 + 0 * C_4
tmp (xxx) = 2 * C_1 + 1 * C_2 + 1 * C_3 + 4 * C_4
tmp (n)
The output should be a matrix with the individual indices.
I have the following code, but I also got the problems, that it won't start at zero.
clc
clear all
G = sort([4.3,10,0.5,6],2);
[numRows,numCols] = size(G);
F = 7;
limit = 3 * F;
n = 0;
for ck = 1:limit
for ckk = 1:limit
for ckkk = 1:limit
for ckkkk = 1:limit
firstC = G(1);
secondC = G(2);
thirdC = G(3);
fourthC =G(4);
tmp = (ck-1)*firstC + (ckk-1)* secondC + (ckkk-1) * thirdC + (ckkkk-1)*fourthC;
if tmp<limit
n = n+1;
N(n) = tmp;
disp([' N( ' num2str(n) ') = ' num2str(ck) '*' num2str(firstC) '+' num2str(ck) ' * ' num2str(secondC) '+' num2str(ckk) ' * ' num2str(thirdC) '+' num2str(ckkk) '*' num2str(fourthC)]);
end
end
end
end
end

Accepted Answer

Matt J
Matt J on 16 Oct 2020
num2str(ck-1)

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!