Strange result of find function

8 ビュー (過去 30 日間)
Johan
Johan 2021 年 9 月 8 日
コメント済み: Johan 2021 年 9 月 8 日
Hello,
I noticed a strange results of the find function today.
I am using the find function to extract the indices of a time serie that is contained inside a structure. The find function does find a result but it is however not the correct one and I do not understand why.
To be clearer here is the code I'm using:
t(1) = 10e-9;
t(2) = 25e-9;
t(3) = 35e-9;
t(4) = 40e-9;
Time_indices = find(abs([files(index).data.time]-t')< eps);
This gives the following result:
Time_indices =
100
651
1152
1603
However, my time series array size is 401x1 double so the indices are wrong apart from the first one. However, if I run the same find function but in a for loop, discretizing the t values I get the expected result:
for i=1:4
test(i) = find(abs([files(index).data.time]-t(i))< eps);
end
test
test =
100 250 350 400
I'm not sure if I'm using find wrong or if there is an issue here, if someone has any insight into this that would be helpful for my understanding.

採用された回答

Jan
Jan 2021 年 9 月 8 日
% Time_indices = find(abs([files(index).data.time]-t')< eps);
% ^
If t and [files(index).data.time] are row vectors, the transposition of t replies a matrix:
a = 1:4;
b = 1:4;
a - b'
ans = 4×4
0 1 2 3 -1 0 1 2 -2 -1 0 1 -3 -2 -1 0
Now the find() command gets the linear indices of the matrix elements.
Maybe using 2 outputs for the find() command solve your needs. Or try ismembertol .
  1 件のコメント
Johan
Johan 2021 年 9 月 8 日
Thanks I see what you mean,
I am creating a 401x4 matrix when doing the -t operation so the find function returns the correct indices but make the 401x4 matrix into a 1604x1 vector, thus the strange results.
Using two outputs does solve the problem.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by