How to determine which of several vectors is larger?
5 views (last 30 days)
Show older comments
I have a vehicle with three batteries, each battery records time, voltage, and current. The timing of each is slightly off so the size of each vector is slightly different, but I want to plot all three so I need to interp1 each of these vectors so they are the same size.
Heres and example of what im doing right now that works but takes a long time:
Given Battery A, B, and C here are my variables:
%Example data
ATime = 1000x1 double
AVoltage = 1000x1 double
ACurrent = 1000x1 double
BTime = 950x1 double
BVoltage = 950x1 double
BCurrent = 950x1 double
CTime = 1010x1 double
CVoltage = 1010x1 double
CCurrent = 1010x1 double
if numel(ATime) > numel(BTime)
BVoltageInterp = interp1(BTime, BVoltage, ATime, 'linear')
BCurrentInterp = interp1(BTime, BCurrent, ATime, 'linear')
end
if numel(ATime) > numel(CTime)
CVoltageInterp = interp1(CTime, CVoltage, ATime, 'linear')
CCurrentInterp = interp1(CTime, CCurrent, ATime, 'linear')
end
if numel(BTime) > numel(ATime)
AVoltageInterp = interp1(ATime, AVoltage, BTime, 'linear')
ACurrentInterp = interp1(ATime, ACurrent, BTime, 'linear')
end
if numel(BTime) > numel(CTime)
CVoltageInterp = interp1(CTime, CVoltage, BTime, 'linear')
CCurrentInterp = interp1(CTime, CCurrent, BTime, 'linear')
end
if numel(CTime) > numel(ATime)
AVoltageInterp = interp1(ATime, AVoltage, CTime, 'linear')
ACurrentInterp = interp1(ATime, ACurrent, CTime, 'linear')
end
if numel(CTime) > numel(BTime)
BVoltageInterp = interp1(BTime, BVoltage, CTime, 'linear')
BCurrentInterp = interp1(BTime, BCurrent, CTime, 'linear')
end
As I mentioned before, it works but is slow. As you can see in this example, A is larger than B but C is larger than A and B, so first the code compares A and B and sees that B is smaller than A so it interps. But eventually it compares B to C and interps again.
What Im thinking (but not sure how to do) is to first find which of the three is larger, then interp the other 4 values to the larger set of vectors, does anyone know of a function that will do this for me? Or even just a faster or more streamlined way to do this? Thanks!
I found a scenario where this doesnt work and now Im back to square 1 trying to figure this out.
For example:
When A = 3, B = 1, and C = 2,
A > B: B = 3
A > C: C = 3
B > A: Nothing happens
B > C: Nothing happens
C > A: Nothing happens
C > B: B = 2
Final: A and C = 3, B = 2
2 Comments
Answers (3)
Star Strider
on 24 Mar 2025
One approach to getting the maximum number of elements and the index —
ATime = linspace(0, 1, 1000).';
BTime = linspace(0, 1, 950).';
CTime = linspace(0, 1, 1010).';
timec = {ATime, BTime, CTime};
[timenumel,idx] = max(cellfun(@numel, timec))
I am not certain where you want to go from there. I assume all the time vectors span the same time as they do here (in this instance, 0 to 1).
.
0 Comments
Voss
on 24 Mar 2025
Given three sets of vectors, where the length within each set is the same but across sets varies, and you want to interp1 to the longest vector as attempted in the code in the question, then:
In order to minimize the number of interp1 calls you can 1) find which of the three sets of vectors (A, B, C) is longest and store the corresponding time vector, then 2) interp1 any vector that's shorter than the longest. [Note that this approach effectively handles cases where more than one set of vectors are the same longest length, e.g., when nA == nB > nC, then only the C vectors are interpolated; when nA == nB == nC, nothing is interpolated.]
nA = numel(ATime);
nB = numel(BTime);
nC = numel(CTime);
[n,idx] = max([nA,nB,nC]);
switch idx
case 1
TimeInterp = ATime;
case 2
TimeInterp = BTime;
case 3
TimeInterp = CTime;
end
if nA < n
AVoltageInterp = interp1(ATime, AVoltage, TimeInterp, 'linear');
ACurrentInterp = interp1(ATime, ACurrent, TimeInterp, 'linear');
else
AVoltageInterp = AVoltage;
ACurrentInterp = ACurrent;
end
if nB < n
BVoltageInterp = interp1(BTime, BVoltage, TimeInterp, 'linear');
BCurrentInterp = interp1(BTime, BCurrent, TimeInterp, 'linear');
else
BVoltageInterp = BVoltage;
BCurrentInterp = BCurrent;
end
if nC < n
CVoltageInterp = interp1(CTime, CVoltage, TimeInterp, 'linear');
CCurrentInterp = interp1(CTime, CCurrent, TimeInterp, 'linear');
else
CVoltageInterp = CVoltage;
CCurrentInterp = CCurrent;
end
0 Comments
Steven Lord
on 24 Mar 2025
Do you want to determine the longest of the vectors, or do you want to interpolate them to a set of "common times"? If so perhaps create a timetable from each of the time and data pairs then synchronize the timetables to a common set of times. Alternately, compute the union of the time vectors and interpolate to that unified time vector. Another option is to compute the bounds of the combined time vector, create a linearly spaced vector using the overall bounds, and interpolate to that linearly spaced vector.
0 Comments
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!