Problems with a for loop

조회 수: 3 (최근 30일)
Antonio Spiere
Antonio Spiere 2020년 10월 16일
답변: Matt J 2020년 10월 16일
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

채택된 답변

Matt J
Matt J 2020년 10월 16일

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by