Hey guys,
I wanted to build an algorithm which should be able to combine different options till a treshold F.
C = sort([4,1,10,6],2);
[numRows,numCols] = size(C);
F = 7;
O= 1;
So now there should be a new matrix U that should look like this:
U(1) = 1 + 1*4; -- the first one standts for the value one in C
U(2) = 1 + 2*4;
U(3) = 1 + 3*4; -- now the treshold of 2*F is reached and it continues with the new number of C = 6
U(4) = 1+ 1*6;
U(5) = 1+2*6;
when all options with C = 1 are calculated the algorithm should continue with the next value of C. In this case 4
U(n) = 4+ 1*1;
U(n+1) = 4+ 2*1;
And so on.
I came up with this:
for i = 1:numCols
while (O > 1) && (N(t) < 2*F)
for t = 1:numCols
N(t) = C(i) + 1 * C(i);
O = O-1;
end
end
end
Any idea how to solve this? This is driving me crazy

4 个评论

hello
i don't understand your first iterations : where does the 4.3 value come from ?
U(1) = 1 + 1*4,3; -- the first one standts for the value one in C
U(2) = 1 + 2*4,3;
U(3) = 1 + 3*4,3; -- now the treshold of 2*F is reached and it continues with the new number of C = 6
you should try explain better how U depends of C (twice) and where indexes are used
Sorry! I meant 4.0 not 4.3. This is the first value of C.
Indices should be like this
U(i) = C(i) + i * C(i) First iteration: U(1) = 1 (from C) + 1 * 4 (from C) Second: U(2) = 1 (from C) + 2 * 4 (from C)
But I can't figure the indices out exactly. I know that I need more then the index i
ok , it's a bit better now
still I need to understand your logic... so can you please confirm / comment my assumptions
let's put in a more general form : U(n) = C(i) + k*C(j)
  • n is the current indice of the main loop (while condition) : goes from 1 to end of validity of while condition
  • k may differ from n , depends if threshold is reached or not
  • i and j indexes are also independant - a contrario from what your code shows. so the first C term is not the same as the second one.
Question : why in the first iteration it starts with C = 4 in U(1) = 1 + 1*4 and not with the first value of C ( C = 1) , it does not follows the same logic as after, once you reach : when all options with C = 1 are calculated the algorithm should continue with the next value of C. In this case 4
U(n) = 4+ 1*1;
U(n+1) = 4+ 2*1;
In this case the "first" C term is updated (1 => 4) , but the second C term restarts at 1 .
so I don't understand the difference in the logic... but you have probably good resons for it ?
"let's put in a more general form : U(n) = C(i) + k*C(j)
  • n is the current indice of the main loop (while condition) : goes from 1 to end of validity of while condition
  • k may differ from n , depends if threshold is reached or not
  • i and j indexes are also independant - a contrario from what your code shows. so the first C term is not the same as the second one. "
U should be a matrix with the stored values after the algorithm for example:
U = [5,9,13...]
The general form is right. But I don`t know with the indice n if this should be the while loop. I considered more the treshold variable k as a while loop.
"Question : why in the first iteration it starts with C = 4 in U(1) = 1 + 1*4 and not with the first value of C ( C = 1) , it does not follows the same logic as after, once you reach : when all options with C = 1 are calculated the algorithm should continue with the next value of C. In this case 4"
Because I don`t need it for my purpose. But maybe the matlab code is easier? What do you think? I could get rid of it afterwards and delete the rows.

请先登录,再进行评论。

 采纳的回答

Mathieu NOE
Mathieu NOE 2020-10-7

0 个投票

hi again
i think I have something for you. I ended up doing it completly different : compute all combinations but keep only the ones that fullfill the threshold criteria. Was much simpler than doing aerobatics with multiple conditions statements.
This is the output of my code : (code is in attachement)
N( 1) = 1+1 * 4
N( 2) = 1+2 * 4
N( 3) = 1+3 * 4
N( 4) = 1+1 * 6
N( 5) = 1+2 * 6
N( 6) = 1+1 * 10
N( 7) = 4+1 * 1
N( 8) = 4+2 * 1
N( 9) = 4+3 * 1
N( 10) = 4+4 * 1
N( 11) = 4+5 * 1
N( 12) = 4+6 * 1
N( 13) = 4+7 * 1
N( 14) = 4+8 * 1
N( 15) = 4+9 * 1
N( 16) = 4+1 * 4
N( 17) = 4+2 * 4
N( 18) = 4+1 * 6
N( 19) = 6+1 * 1
N( 20) = 6+2 * 1
N( 21) = 6+3 * 1
N( 22) = 6+4 * 1
N( 23) = 6+5 * 1
N( 24) = 6+6 * 1
N( 25) = 6+7 * 1
N( 26) = 6+1 * 4
N( 27) = 6+1 * 6
N( 28) = 10+1 * 1
N( 29) = 10+2 * 1
N( 30) = 10+3 * 1

3 个评论

Thanks! I will try it tomorrow. My PC just deleted my whole Data SSD
Thank you very very much! It works and I can continue my work. I'm adding some things to the code, but this is how it should work. I really appreciate your help
glad it helped you !! good success in your activities !

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2019a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by