필터 지우기
필터 지우기

Create a sequence based on other arrays

조회 수: 1 (최근 30일)
luca
luca 2019년 10월 3일
댓글: Nipun Katyal 2020년 3월 2일
Given the arrays
SP = [1 2 4 6 7 9 10]
V = [5 20 10 15 5 20 10 ]
I want to create a random array P that contain the elements of SP repeated as many times as is indicated in V. So the element one has to be repeated 5 times, two repeated 20 times, four repeated 10 times and so on.
An important rule to respect is that at maximum we can mix two elements at time, respecting the order indicated in V. So the first elements in the random sequence will be 1 and 2, 4 can appear just when one between 1 and 2 will be terminated.
Let's see an example:
P = [1 1 2 1 2 1 2 2 1 2 2 4 2 4 2 4 2 4 4 2 2 4 2 2 4 4 2 2 4 4 2 6 2 6 2 6 6 2 6 2 6 6 7 6 7 7 6 6 7 6 6 7 6 9 6 9 6 9 9 10 10 9 9 10 9 9 10 9 9 10 9 9 10 9 9 10 9 9 10 10 9 9 10 9 9]
As we can see as soon as 1 has been repeated 10 times, then 4 appears in the sequence in combination with 2. Then again is 4 that terminated before 2 and 6 enters in the sequence with 2.
May someone help me with this task?

답변 (1개)

Nipun Katyal
Nipun Katyal 2020년 3월 2일
You can do this by maintaining two variables to store the current two indices until the correponding elements in V are exhausted, I hope this helps.
  댓글 수: 2
Guillaume
Guillaume 2020년 3월 2일
if(idx <= maxElem && idx< maxElem)
I suspect that the second idx is meant to be idy. Otherwise it doesn't make much sense, the second is always true if the first half is.
Note that in matlab you don't need to enclose if expressions in ().
Nipun Katyal
Nipun Katyal 2020년 3월 2일
Yes it is supposed to be idy

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by