Extending the values of a vector
2 views (last 30 days)
Jyothi Alugolu on 23 Feb 2018
I have a vector of size 1*6 with values A=[2 14 6 18 9 10].Now i want to convert this vector to another vector of size 1*12(for suppose) with the values to be distributed across the vector. Like for decreasing the vector we are having accumarray (In that it will add the values in the same indices) .
Walter Roberson on 23 Feb 2018
B1 = ceil(rand(size(A)) .* (A-1));
B2 = A - B1;
B = [B1, B2];
Note: with this particular code, if there is an element of A which is exactly 1, then the 1 will always be allocated to the second half and the first half will always have 0 in the corresponding location. With this code, that is the only time that a 0 can occur.
If you need the 1 to be split randomly between the two halves then the easiest way to do that without special-casing 1 would involve a possibility that 0 would occur in other locations.