Create two arrays on the basis of other arrays

1 次查看(过去 30 天)
Given
SP = [1 2 4 6 7 9 10]
V = [5 20 10 15 5 20 10 ]
I want to obtain the two following two arrays
A = [1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 10 10 10 10 10 10 10 10 10 10]
B= [2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 ]
That is, starting from 1, I position one in the first vector A as many times as is indicated in V. then I switch to 2 and position it in B as many times as is indicated in V. Then I take 4 and put in the vector (A or B) that is less empty, so A. then again with 6, I put it in A again becuase its length is less then the one of B and so on with the other elements till obtaing the final vector A and B

采纳的回答

Guillaume
Guillaume 2019-10-3
编辑:Guillaume 2019-10-3
%first find which of A or B elements are going into. Only depends on V
isA = false(size(V)); %put in A (true) or B (false)
lA = 0; lB = 0; %current length of A and B
for vi = 1:numel(V)
if lA <= lB %put in A
lA = lA + V(vi);
isA(vi) = true;
else %put in B
lB = lB + V(vi);
end
end
A = repelem(SP(isA), V(isA))
B = repelem(SP(~isA), V(~isA))
  5 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by