Extending the values of a vector

4 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2018-2-27
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-2-23
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 个评论
Jyothi Alugolu
Jyothi Alugolu 2018-2-27
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 2018-2-27
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.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2018-2-23
A=[2 14 6 18 9 10]
half1 = arrayfun(@(v) randi(v-1), A);
full = [half1, A-half1]
result = full(randperm(numel(full)))

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by