Borrar filtros
Borrar filtros

Extending the values of a vector

4 visualizaciones (últimos 30 días)
Jyothi Alugolu
Jyothi Alugolu el 23 de Feb. de 2018
Comentada: Walter Roberson el 27 de Feb. de 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 comentarios
Guillaume
Guillaume el 27 de Feb. de 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.
Jyothi Alugolu
Jyothi Alugolu el 27 de Feb. de 2018
Please don't delete

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Feb. de 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 comentarios
Jyothi Alugolu
Jyothi Alugolu el 27 de Feb. de 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 el 27 de Feb. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

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

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by