필터 지우기
필터 지우기

Create two arrays on the basis of other arrays

조회 수: 2 (최근 30일)
luca
luca 2019년 10월 3일
댓글: luca 2019년 10월 3일
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
luca
luca 2019년 10월 3일
No they are two different things Star. I'm trying to following different ways to apporach the problem

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

채택된 답변

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
luca
luca 2019년 10월 3일
is there another mistake?
luca
luca 2019년 10월 3일
oh yes, SP . Thanks Guillaume

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

추가 답변 (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