Create two arrays on the basis of other arrays

1 visualización (últimos 30 días)
luca
luca el 3 de Oct. de 2019
Comentada: luca el 3 de Oct. de 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 comentarios
luca
luca el 3 de Oct. de 2019
No they are two different things Star. I'm trying to following different ways to apporach the problem

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 3 de Oct. de 2019
Editada: Guillaume el 3 de Oct. de 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 comentarios
luca
luca el 3 de Oct. de 2019
is there another mistake?
luca
luca el 3 de Oct. de 2019
oh yes, SP . Thanks Guillaume

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by