MATLAB Answers

Error using interp1>re​shapeAndSo​rtXandV (line 424) X and V must be of the same length.

17 views (last 30 days)
Megan on 9 Oct 2019
Edited: Megan on 19 Feb 2020
I geht this error message when I try to derive my acceleration of the car. I think there is a size problem, but I don't know how to solve it.
The data is measured from a car.


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 9 Oct 2019
Edited: Stephen Cobeldick on 9 Oct 2019
Assuming that and meas.acc.time are vectors with the same number of elements, then diff on the data vector will return a numeric vector with one element fewer than the time vector, so and meas.jerk.time will likely have mismatching sizes.
One easy solution is to take the time as the mean of adjacent time points, e.g.:
meas.jerk.time = (meas.acc.time(1:end-1) + meas.acc.time(2:end)) ./ 2;


Show 3 older comments
Megan on 10 Oct 2019
meas.jerk.time = (meas.acc.time(1:end-1) + meas.acc.time(2:end)) ./ 2;
Could you maybe explain this part of the code? I didn't get it.
What makes 1:end-1? What is end and what do you mean with it?
I got, that my vektor is smaler then my time vektor. I got this part. How do you get the average adjacent time points? I mean could you explain this little code :) I'm really new with matlab I'm sorry
Stephen Cobeldick
Stephen Cobeldick on 10 Oct 2019
"What is end and what do you mean with it?"
end is the index of the last element in that vector.
Because end is the index of the last element, end-1 is the index of the second-to-last element. Thus
uses basic MATLAB indexing to return a subvector of meas.acc.time, from its first element to its second-to-last element. Thus the subvector has one fewer element than meas.acc.time, and therefore exactly the same number of elements as the output of diff.
"How do you get the average adjacent time points?"
Simply by using the definition of arithmetic mean of two values:
which is commonly written as (val1 + val2) / 2
Compare with my answer, which has the form (vec1 + vec2) ./ 2 , where vec1 is the subvector from the first element to the second-to-last element, and vec2 is the subvector from the second element to the last element. Then these two vectors are added together (i.e. corresponding elements are summed), and then divided by two to give the arithmetic mean.
V % [a,b,c,d,e]
V(1:end-1) % [a,b,c,d]
V(2:end) % [b,c,d,e]
V(1:end-1)+V(2:end) % [a,b,c,d]+[b,c,d,e] = [a+b,b+c,c+d,d+e]
(V(1:end-1)+V(2:end))/2 % [a+b,b+c,c+d,d+e]/2 = [(a+b)/2,(b+c)/2,(c+d)/2,(d+e)/2]
Tip: to use MATLAB you need to learn how to use indexing, and work with vectors/arrays.
Note that you do no have to use the arithmetic mean. As I wrote in my answer, only you can decide if such an averaging is appropriate, or if another type of averaging would be better.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!