Index location of certain dates with find and a loop

2 ビュー (過去 30 日間)
Laura Szczyrba
Laura Szczyrba 2022 年 8 月 24 日
コメント済み: David Hill 2022 年 8 月 25 日
Trying to create a simple loop that identifies the index locations where each particular date (TargetDates) matches the date in a larger array of dates (t_list)
t_list --> 1924x1 double
TargetDates --> 267x1 double
----------------------------------------------------------------------------
locs= zeros(length(TargetDates),1); % initialize array of locations, size 267x1
for ii = 1:size(TargetDates,1) % for each date listed in TargetDates (1:267)
locs(ii) = find(round(t_list,6) == TargetDates(ii,1)); % find the index location where rounded t_list matches the date in TargetDates
end
--------------------------------------------------------------------------------
I receive the error: Unable to perform assignment because the left and right sides have a different number of elements.
I am sure it is a silly error that I am just not seeing. Thanks in advance for any advice!

回答 (2 件)

David Hill
David Hill 2022 年 8 月 24 日
[~,idx]=ismember(TargetDates,t_list);%only provides first occurance
  2 件のコメント
Laura Szczyrba
Laura Szczyrba 2022 年 8 月 25 日
編集済み: Laura Szczyrba 2022 年 8 月 25 日
I need the location where all TargetDates (each date in TargetDates) match t_list, not just the first occurance, which is why I was setting up the loop!
David Hill
David Hill 2022 年 8 月 25 日
You can see without a good description and sample data set we are guessing what you want or making wrong assumptions.

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


VBBV
VBBV 2022 年 8 月 25 日
locs= zeros(length(TargetDates),length(t_list)); % change preallocated array size
Change the preAllocated size of locs
locs(ii,:) = find(round(t_list,6) == TargetDates(ii,1)); % find returns a vector of indices that match the condition
As you are comparing the whole t_list with TargetDates you can do above
  3 件のコメント
VBBV
VBBV 2022 年 8 月 25 日
for ii = 1:size(TargetDates,1)
Use the above in for loop. What is size of TargetDates?
VBBV
VBBV 2022 年 8 月 25 日
It seems you are using DateNumber

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by