Extending the values of a vector

조회 수: 2 (최근 30일)
Jyothi Alugolu
Jyothi Alugolu 2018년 2월 23일
댓글: Walter Roberson 2018년 2월 27일
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.
Jyothi Alugolu
Jyothi Alugolu 2018년 2월 27일
Please don't delete

댓글을 달려면 로그인하십시오.

채택된 답변

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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by