Effacer les filtres
Effacer les filtres

Create two arrays on the basis of other arrays

2 vues (au cours des 30 derniers jours)
luca
luca le 3 Oct 2019
Commenté : luca le 3 Oct 2019
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
  2 commentaires
luca
luca le 3 Oct 2019
No they are two different things Star. I'm trying to following different ways to apporach the problem

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 3 Oct 2019
Modifié(e) : Guillaume le 3 Oct 2019
%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 commentaires
luca
luca le 3 Oct 2019
is there another mistake?
luca
luca le 3 Oct 2019
oh yes, SP . Thanks Guillaume

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by