How to repeat element of array to complete Specific shape In matlab

3 ビュー (過去 30 日間)
Med Future
Med Future 2022 年 3 月 10 日
編集済み: Star Strider 2022 年 3 月 11 日
Hello Everyone i hope you are doing well. I have the following dataset
One has 30 samples ( Shape 30x1) and other has 115 samples(Shape 115x1) . My network is trained on shape (1000x1). I want to repeat elements of the array from start to complete 1000 samples.
The samples varries (10-1000) like shape is 10x1 or 388x1
for example the data is look like that, so it will repeat from start to complete 1000 samples
How can i do it in MATLAB

採用された回答

Voss
Voss 2022 年 3 月 10 日
編集済み: Voss 2022 年 3 月 10 日
You can use repmat() to replicate the array (or repelem() to repeat each element of the array) enough times to get at least 1000 rows, then remove the extra.
load('dataset1.mat');
load('dataset2.mat');
N_needed = 1000;
dataset1_new = repmat(dataset1,ceil(N_needed/size(dataset1,1)),1);
dataset1_new(N_needed+1:end,:) = [];
dataset2_new = repmat(dataset2,ceil(N_needed/size(dataset2,1)),1);
dataset2_new(N_needed+1:end,:) = [];
whos
Name Size Bytes Class Attributes N_needed 1x1 8 double ans 1x37 74 char dataset1 30x1 240 double dataset1_new 1000x1 8000 double dataset2 115x1 920 double dataset2_new 1000x1 8000 double

その他の回答 (1 件)

Star Strider
Star Strider 2022 年 3 月 10 日
編集済み: Star Strider 2022 年 3 月 11 日
EDIT — (11 Mar 2022 at 6:14)
There is one other way I can think of to do this, however it involves some compromises since it ‘squeezes’ the 1020-element vector into 1000 elements, or ‘stretches’ the 990-element vector to 1000 elements (both will work with this code, although I am only showing one here):
x1 = 1:numel(stretch1); % Match 'stretch1' Length
xq = linspace(1, numel(stretch1), DesiredLength); % 'DesiredLength' Interpolation Vector
squeeze1 = interp1(x1, stretch1, xq); % Interpolate (Decimal Fractions)
squeeze1i = round(squeeze1); % Integers Only
figure
subplot(3,1,1)
plot(x1, stretch1)
xlim([1 numel(stretch1)])
grid
title(sprintf('Original Vector, Length = %d',numel(stretch1)))
subplot(3,1,2)
plot(xq, squeeze1)
xlim([1 numel(stretch1)])
grid
title(sprintf('Squeezed Vector, Length = %d',numel(squeeze1)))
subplot(3,1,3)
plot(xq, squeeze1i)
xlim([1 numel(stretch1)])
grid
title(sprintf('Squeezed Integer Vector, Length = %d',numel(squeeze1i)))

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by