Making Different Length Vector Same Length

I have two sets of data, stress and strain. Strain is [9x1] and stress is [12x1]. I need both vectors to be the same length, which would be [12x1]. I have tried using the interp1 function, but in order to use this it seems as though the inputs, which would be stress and strain, have to be the same size already. I am not sure if this is even the right direction. I have provided my raw stress and strain data if someone would like to help or provide any sample code. Thank you in advance!
stress = [1.854621961
397.953227
680.5854525
1319.335498
1569.283812
2489.328323
3585.957167
4591.466307
5916.213652
7592.943924
8625.360283
9808.244251];
strain = [0.001170792
0.005019681
0.010524187
0.017380968
0.026422366
0.034377908
0.04229767
0.050683195
0.059544219
];

 Risposta accettata

Star Strider
Star Strider il 22 Feb 2019
Modificato: Star Strider il 22 Feb 2019
One approach:
strainNew = interp1((1:numel(strain)), strain, linspace(1, numel(strain), numel(stress)), 'linear')';
figure
plot((1:numel(strain)), strain)
hold on
plot(linspace(1, numel(strain), numel(stress)), strainNew)
hold off
It creates a 12-element vector with the same beginning and ending values as the original vector to do the interpolation.
EDIT — Added transpose operator (') to create (12 x 1) vector,, added plot. Original code otherwise unchanged.

5 Commenti

@Katherine Hollar — Extrapolating is straightforward.
Try this:
strainNew = interp1((1:numel(strain)), strain, (1:numel(stress)), 'linear','extrap')';
There are other interpolation methods available as well. If you want to extrapolate, it is necessary to specify a method.
Okay, that makes sense.
For the interpolation code that you provided though. I am noticing that the trend is not the same when plotted. Shouldn't the interpolated data follow the same trend as the original data, but just have more points now?
Stress-Strain.PNG
I am not certain what you are referring to.
This code:
strainNew = interp1((1:numel(strain)), strain, linspace(1, numel(strain), numel(stress)), 'linear')';
figure
plot((1:numel(strain)), strain, '-b')
hold on
plot(linspace(1, numel(strain), numel(stress)), strainNew, '-+r')
hold off
produces this plot:
Making Different Length Vector Same Length - 2019 02 22.png
The original and interpolated data line up closely.
The extrapolated data also line up appropriately:
strainNewExtrap = interp1((1:numel(strain)), strain, (1:numel(stress)), 'linear','extrap')';
figure
plot(strain, stress(1:numel(strain)), '-b')
hold on
plot(strainNewExtrap, stress, '--r')
hold off
Making Different Length Vector Same Length (2) - 2019 02 22.png
The original interpolated vector does not match closely with the original because they are different vectors:
figure
plot(strain, stress(1:numel(strain)), '-+b')
hold on
plot(strainNew, stress, '-+r')
hold off
legend('Original','Interpolated', 'Location','NW')
xlabel('Stress')
ylabel('Strain')
Making Different Length Vector Same Length (3) - 2019 02 22.png
It depends on what result you want.
Okay, I think we are on the same page. Thanks for all the help. I will have to evaluate which option, interpolation or extrapolation, will most accurately represent my data for this project. Thanks again!
As always, my pleasure!

Accedi per commentare.

Più risposte (2)

Jos (10584)
Jos (10584) il 22 Feb 2019
Modificato: Jos (10584) il 22 Feb 2019
[EDITED] This might work for you:
ix = linspace(1, numel(stress), numel(strain))
NewStrain = interp1(ix, strain, 1:numel(stress))

2 Commenti

I believe that only gives me a [9x1], instead of the [12x1] I need for strain to be in order to match stress.
I mixed up strain and stress ...

Accedi per commentare.

KH
KH il 22 Feb 2019
That is an interesting solution! I do have one question though. Because the code you provide assumes that the maximum strain value is the last one present, is it not possible to create a code that can interpolate further than this value. With this data, I am assuming the last value would be slightly greater than the last value provided, 0.0595.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by