if difference between two values is smaller than replace by
12 ビュー (過去 30 日間)
古いコメントを表示
I have a vector that contains several time values. For the application I need to check if the difference between two values is smaller than 3e-6. If that's the case the second (higher) value shall be replaced with the first.
My approach would be for-loops but maybe there is a smarter and more efficient way to solve this?
5 件のコメント
the cyclist
2023 年 10 月 22 日
Is each column processed independently?
It doesn't seem that any value is greater than the immediately preceding value by less than 3.e-6, but more than zero:
load("time.mat","time")
any((diff(time) > 0) & (diff(time) < 3.e-6),"all")
Walter Roberson
2023 年 10 月 22 日
編集済み: Walter Roberson
2023 年 10 月 22 日
load("time.mat","time")
dt = diff(time);
dt = dt(dt ~= 0);
min(dt)
So in all cases where the difference in times is < 3e-6, the two values are already identical.
採用された回答
Pratyush
2023 年 10 月 23 日
Hi Kai,
I understand that you have a vector, and if the difference between any two values is less than 3e-6, you want to set them both equal to the lesser of the two numbers. The following script could be used for that purpose:
% Sort timeValues vector
timeValues = sort(timeValues);
% Calculate the differences between adjacent values
diffValues = diff(timeValues);
% Find the indices where the differences are smaller than 3e-6
replaceIndices = find(diffValues < 3e-6);
% Replace the second value with the first for the identified indices
timeValues(replaceIndices + 1) = timeValues(replaceIndices);
Hope this helps.
1 件のコメント
Dyuman Joshi
2023 年 10 月 23 日
Note that this answer sorts the data before finding difference between consecutive elements.
OP has/had not specified anything in regards to that.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!