Extending the values of a vector
2 views (last 30 days)
Show older comments
Jyothi Alugolu
on 23 Feb 2018
Commented: Walter Roberson
on 27 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) .
2 Comments
Guillaume
on 27 Feb 2018
It's a shame that whichever answer that had a very long list of comments got deleted, as all the context for the remaining answers is now missing.
Please, whoever deleted their answer, don't delete answers that have a long discussion attached to it as that discussion was very useful in explaining what was wanted.
Accepted Answer
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.
2 Comments
Walter Roberson
on 27 Feb 2018
Splitting into variable sizes is only possible with clear rules about which slot is to be split into which, unless you are willing to give up the rule that each source slot's contribution has to be split into fixed locations. You also need to to specify what should happen if the value for any given source slot is less than the number of of slots it is to be distributed into, so that I don't have to invent my own rules about handling that edge case.
More Answers (1)
Guillaume
on 23 Feb 2018
A=[2 14 6 18 9 10]
half1 = arrayfun(@(v) randi(v-1), A);
full = [half1, A-half1]
result = full(randperm(numel(full)))
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!