Take only some values from one array

3 ビュー (過去 30 日間)
Carmine Schiavone
Carmine Schiavone 2021 年 7 月 16 日
コメント済み: dpb 2021 年 7 月 16 日
Hello everybody, I have an array of datas "x" and an array of time "t" that have some dimension like 30000x1, now I need just few values of this array x at some specific times.
I have this array of time called "t1" that is a long list of instants of time like 200x1 and I want to select the value of "x" corresponding only to the instants in "t1".
At the beginning I tryed to do as follows but Matlab says that this can't be done because the matrix dimensions must agree
time= t==t1;
x= x(time);
  1 件のコメント
dpb
dpb 2021 年 7 月 16 日
ismember

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

回答 (1 件)

J. Alex Lee
J. Alex Lee 2021 年 7 月 16 日
you can't compare arrays of different lengths. a straightforward but cumbersome way is to cycle through each of your t1 and find the matches
x1 = nan(size(t1));
for i = 1:numel(t1)
tcheck = t1(i)
mask = (tcheck==t)
if sum(mask)==1
x1(i) = x(mask);
end
end
Or you can use something like ismember to get the locations
[~,idx] = ismember(t1,t)
x1 = x(idx)
if your times in t1 aren't exactly the same as times in t, then you can use "ismembertol"

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by