How to make a vector linearly spaced?
Show older comments
Hello everyone,
I have generated the timestamps for the charging power of a battery in the form of:
t=[7.2285 8.2285 16.4685 ...]
I have been trying to make this data linearly spaced, as in:
t2=[0.0000 0.0001 ... 7.2285 ... 8.2285 ... 16.4685]
I've tried the index method to find the index of the matching values as follows:
t3=0:0.0001:24;
for m=1:length(t3)
ind=find(t(:,1)==t3(m,1),1);
end
It always returns a zero matrix.
I can then use the index of the matching values to find the corresponding values in another vector. I've been at it for the last three hours, but I can't seem to find a matching value between the two vectors.
Is there a way to simply add linearly increasing values in the vector?
Accepted Answer
More Answers (2)
Guillaume
on 5 Jul 2016
As Stephen's says, your t3 and t don't match because of floating point errors. You need to use a tolerance to perform the comparison instead of pure equality. I.e. instead of
a == b
use
abs(a-b) <= some_tolerance
However, since you want to find the location of elements of a vector within another vector, allowing some tolerance for equality, there is now (since 2015a) ismembertol. This will return all your indices in one go, no need for a loop:
t3 = 0:0.0001:24
[found, indices] = ismembertol(t, t3) %use default relative tolerance of 1e-12
assert(all(found), 'some elements of t were not found in t3 within the given tolerance')
Thorsten
on 5 Jul 2016
I cannot reproduce the error. This works fine for me:
t=[7.2285 8.2285 16.4685];
t2 = 0:0.0001:t(end);
[f idx] = ismember(t, t2)
Categories
Find more on MATLAB 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!