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

조회 수: 2 (최근 30일)
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
[335
385
227
185
184
462]
How can i do it in MATLAB

채택된 답변

Star Strider
Star Strider 2022년 3월 10일
The desired result is not obvious. Simply repeating with repmat will not work because both data set lengths are not integral divisors of 1000 so the last copy will be incomplete in that instance.
One option is to interpolate —
LD = load('dataset1.mat');
dataset1 = LD.dataset1;
xq = linspace(1, numel(dataset1), 1000);
method = 'nearest';
stretch1 = interp1((1:numel(dataset1))',dataset1, xq', method);
See if that does what you want. If so, do it with ‘dataset2’ as well.
See the documentation for interp1 to consider other method options if 'nearest' does not give the desired result.
.
  댓글 수: 5
Med Future
Med Future 2022년 3월 10일
@Star Strider what i have to do to remove extra value which is greater then 1000?
Star Strider
Star Strider 2022년 3월 10일
편집: Star Strider 2022년 3월 11일
That is straightforward —
LD = load('dataset1.mat');
dataset1 = LD.dataset1;
DesiredLength = 1000;
repeats = ceil(DesiredLength/numel(dataset1)) % Use 'floor' Or 'ceil'
stretch1 = repmat(dataset1, repeats, 1);
stretch1 = stretch1(1:DesiredLength); % Snip At 'DesiredLength' To Remove Extra Elements
There are also other ways, although that is the easiest.
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)))
.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by