help: creating vectors.

조회 수: 13 (최근 30일)
Pasqualina Verolino
Pasqualina Verolino 2017년 3월 15일
편집: Jan 2017년 3월 15일
I have to create a vector, which returns 4 for the first 30 numbers, 5 for the next 31, the next 6 to 30 etc. etc. in particular, it must give me a number from 4 to 14, repeated for a number of times equal to 30, 31 or 28, in the order (30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28), or 30 4 times, 31 5 times, 30 6 times, etc. etc

답변 (2개)

KSSV
KSSV 2017년 3월 15일
If you want 31 5 times, 30 six times use like below:
[31*ones(1,5) 30*ones(1,6)]
You can add whatever you want in the above code similarly.

Jan
Jan 2017년 3월 15일
편집: Jan 2017년 3월 15일
This is a job for repelem:
Rep = [30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28];
Value = 4:(3+length(Rep)); % Test data
R = repelem(Value, Rep);
This was introduced in R2015a. If you have an older version, you can either use FEX: RunLength:
RunLength(Value, Rep)
Or program it explicitly:
len = length(Rep); % Number of bins
d = cumsum(Rep); % Cummulated run lengths
index = zeros(1, d(len)); % Pre-allocate
index(d(1:len-1)+1) = 1; % Get the indices where the value changes
index(1) = 1; % First element is treated as "changed" also
R = Value(cumsum(index));
  댓글 수: 1
Pasqualina Verolino
Pasqualina Verolino 2017년 3월 15일
wow it fix perfect! thank u so much!

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by