I have created an "array" of sorts using
For[i = 1, i <= Length[vector], i++,
codes[i] = {}
];
The vector is a vector of probabilities, the sum of the vector is 1. I want to start halfing the vector and after each halfing, I want to add a number to the appropriate codes[i]
variable. Meaning I half the vector, then I half the halves, etc. etc., until only individuals are left, and that is where the program ends, with each codes[i]
filled with appropriate numbers.
The halfing is done based on the sums of probabilities, not the length of the vector, so a 6 item vector
could be halfed into 2 and 4 items.
I have tried looping the halfing, but of course, it cuts it, assigns numbers, and then starts halfing the first half, and ignores the second, from where it progresses, and only a small part of the vector is done correctly in the end.
total = 1;
sum = 0;
For[i = 1, sum < (total/2), i++,
sum = sum + vector[[i]];
cut = i;
];
To cut a vector in half I run a cycle, summing the probabilities in the vector until the sum is greater or equal to half of what I need. The value of total is 1, because the sum of all the probabilities is 1, therefore I run a cycle adding up until the sum is greater than 0.5, then I save the index of the last number, and I run more cycles that fill the codes[i] vectors with numbers. I fill them with 0s and 1s, so I fill codes[i] with 0s up until the cut variable, then I fill the rest with 1s.
Is it possible to make such a loop, where it completes the entire vector?
codes[i]
vectors with numbers. I fill them with 0s and 1s, so I fillcodes[i]
with 0s up until thecut
variable, then I fill the rest with 1s. $\endgroup$vector = {.2, .1, .4, .3}
? $\endgroup$cut=3
and splitvector
into two piecesvector2 ={{0.2, 0.1, 0.4}, {0.3}}
... then what? $\endgroup$