Extending the values of a vector

3 views (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

Walter Roberson
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
Jyothi Alugolu
Jyothi Alugolu on 27 Feb 2018
Thank u @Walter Roberson.Is it possible to split the values if A is of size 1*93 and converted vector is of size 1*160 i.e., in the above we considered the resultant vector size is double the size of original.What we have to do if the size is varying????
Walter Roberson
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.

Sign in to comment.

More Answers (1)

Guillaume
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)))

Categories

Find more on Multidimensional Arrays 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!