Problems with a for loop

3 vues (au cours des 30 derniers jours)
Antonio Spiere
Antonio Spiere le 16 Oct 2020
Réponse apportée : Matt J le 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

Réponse acceptée

Matt J
Matt J le 16 Oct 2020

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by