How to create a vector that repeats 5 values per increment in a for loop?

13 ビュー (過去 30 日間)
Brianna Miranda
Brianna Miranda 2023 年 1 月 20 日
回答済み: VBBV 2023 年 1 月 20 日
I have a vector I am trying to create that contains the numbers 100-1000 and is incremented by 50. But I want the vector to contain 5 of each increment like [100,100,100,100,100,150,150,150,150,150,200,200,200,200,200,.......,1000,1000,1000,1000,1000]. I created the for loop below but it creates a matrix with 5 rows of 1000. How can I change this for loop to get the vector described above?
for ii=100:50:1000
for j=1:19
x = zeros(1,5)+ii;
ratesX(:,j) = x;
end
end

採用された回答

VBBV
VBBV 2023 年 1 月 20 日
x = zeros(1,5);
for ii=100:50:1000
x = [x ones(1,5)*ii];
end
x(x==0) = []
x = 1×95
100 100 100 100 100 150 150 150 150 150 200 200 200 200 200 250 250 250 250 250 300 300 300 300 300 350 350 350 350 350

その他の回答 (1 件)

the cyclist
the cyclist 2023 年 1 月 20 日
v = 100:50:1000;
u = repelem(v,5);

カテゴリ

Find more on Specialized Power Systems in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by