Filter löschen
Filter löschen

Create two arrays on the basis of other arrays

2 Ansichten (letzte 30 Tage)
luca
luca am 3 Okt. 2019
Kommentiert: luca am 3 Okt. 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 Kommentare
luca
luca am 3 Okt. 2019
No they are two different things Star. I'm trying to following different ways to apporach the problem

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 3 Okt. 2019
Bearbeitet: Guillaume am 3 Okt. 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 Kommentare
luca
luca am 3 Okt. 2019
is there another mistake?
luca
luca am 3 Okt. 2019
oh yes, SP . Thanks Guillaume

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by