# How can I make multiple vectors the same length?

1 view (last 30 days)
Christoph Centner on 16 Sep 2020
Edited: Stephen Cobeldick on 16 Sep 2020
Hi all,
I am struggeling with an interpolation task and dont make any process.
I have calculated muscle thickness values across the total muscle length of several subjects. Now I want to compare the region specific differences between subjects but of course all subjects have different muscle length (and thus I have vectors with more values (if muscle has greater length) and smaller vectors (for short muscles).
How could I interpolate these data so that each vector has the same length? I have attached a data sheet (each subjects has an own column).

Walter Roberson on 16 Sep 2020
Do you want the interpolation to be done at intervals that are the same for each muscle (for example 2mm), or do you want the interpolation to be at intervals that are equal proportions of the muscle (e.g., every 1/8th of the length) ?
Christoph Centner on 16 Sep 2020
Hey Walter,
thank you for your quick reply. Ideally, I would like to have it done at equal portions (10%, 20%, 30%,..) of muscle length.
Do you know any solution?
Thanks so much!

Stephen Cobeldick on 16 Sep 2020
Edited: Stephen Cobeldick on 16 Sep 2020
Because each of the input columns has their own domain, you will have to loop over the columns and interpolate each column separately, something like this:
out = inp; % preallocate
num = size(out,1);
for k = 1:size(out,2)
idx = ~isnan(inp(:,k));
xi = 1:nnz(idx);
xo = linspace(1,nnz(idx),num);
out(:,k) = interp1(xi,inp(idx,k),xo);
end
Giving:
>> inp % raw data
inp =
0.3487 0.6447 0.8550 0.6123 0.4617 0.7680 0.8120 0.4800 0.9830
0.7033 0.5983 0.8143 0.7540 0.6167 0.6230 0.6570 0.6357 0.7357
0.7700 0.6753 0.2363 0.7473 0.7600 0.6143 0.8490 0.8643 0.6917
0.7583 0.7740 0.7683 0.9083 0.7167 0.5497 0.8620 0.8520 0.8227
0.8963 0.8700 0.8983 0.9853 0.8053 0.6887 0.9170 0.9657 0.9020
0.8990 1.0077 0.9497 0.9460 0.8480 0.7230 0.8993 1.0130 0.9747
0.9040 0.9130 0.9560 1.0037 0.8913 0.7640 0.9340 1.1217 1.0367
0.8550 0.9693 0.9565 1.0513 0.8900 0.7763 1.0707 1.1203 1.0160
0.9277 0.9963 0.9220 NaN 0.9037 0.8657 1.1093 1.1407 1.0457
0.9290 1.0790 0.8823 NaN 0.8880 0.8247 1.0657 1.1763 1.0410
NaN 0.9413 0.7607 NaN 1.0247 NaN 1.0347 1.1750 1.0693
NaN NaN NaN NaN 0.9120 NaN 1.0880 1.2743 1.0937
NaN NaN NaN NaN NaN NaN 1.1810 1.0443 1.1110
NaN NaN NaN NaN NaN NaN 1.2476 NaN NaN
NaN NaN NaN NaN NaN NaN 1.3207 NaN NaN
>> out % interpolated data
out =
0.3487 0.6447 0.8550 0.6123 0.4617 0.7680 0.8120 0.4800 0.9830
0.5767 0.6116 0.8260 0.6832 0.5835 0.6748 0.6570 0.6134 0.7710
0.7224 0.6313 0.5666 0.7540 0.6986 0.6205 0.8490 0.7990 0.7042
0.7652 0.6894 0.3123 0.7507 0.7445 0.6150 0.8620 0.8573 0.7665
0.7633 0.7599 0.6923 0.7473 0.7293 0.5774 0.9170 0.9007 0.8567
0.7879 0.8289 0.8426 0.8278 0.7990 0.5795 0.8993 0.9792 0.9228
0.8766 0.9093 0.9130 0.9083 0.8358 0.6688 0.9340 1.0285 0.9835
0.8977 1.0077 0.9497 0.9468 0.8697 0.7058 1.0707 1.1217 1.0367
0.8997 0.9400 0.9542 0.9853 0.8910 0.7289 1.1093 1.1205 1.0190
0.9029 0.9371 0.9562 0.9657 0.8910 0.7552 1.0657 1.1349 1.0372
0.8830 0.9732 0.9515 0.9460 0.9017 0.7693 1.0347 1.1610 1.0430
0.8602 0.9925 0.9269 0.9748 0.8936 0.7827 1.0880 1.1758 1.0531
0.9069 1.0436 0.8993 1.0037 0.9466 0.8401 1.1810 1.2034 1.0763
0.9281 1.0397 0.8476 1.0275 1.0005 0.8510 1.2476 1.2415 1.0961
0.9290 0.9413 0.7607 1.0513 0.9120 0.8247 1.3207 1.0443 1.1110
>>

Show 1 older comment
Ameer Hamza on 16 Sep 2020
Just change these two lines
out = inp; % preallocate
num = size(out,1);
with
num = 10;
out = zeros(num, size(inp,2)); % preallocate
Christoph Centner on 16 Sep 2020
Thank you Steven! Much appreciated!!!!
Stephen Cobeldick on 16 Sep 2020
"Thank you Steven!"
Thank you Ameer Hamza.
"Do you also know how to make them all a length of 10 data points? Then I could easily compare all subjects on 10%, 20%, 30% - 100% of muscle length?"
If you sample every 10% along the length, i.e. at 0%, 10%, 20%, ... 90%, 100% of the length, then this requires eleven sample points, not ten. If you sample the length with only ten points, then this gives samples at 0%, 11.111..%, 22.222..%, ... 88.888...%, 100% of the length.

### Community Treasure Hunt

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

Start Hunting!