필터 지우기
필터 지우기

Find a match in a structure array using arrayfun

조회 수: 17 (최근 30일)
Alexander
Alexander 2014년 6월 4일
댓글: dpb 2014년 6월 6일
Hi there,
I have some data stored in a structure array s, with the following fields: s.Month, s.Day, s.Hour and s.Data. The structure array has the size of 1x123, i.e.a time series of 123 time steps.
I try to find a certain set within the structure array, matching the date, say 08/21 00 UTC.
My idea was:
m = 8; d = 21; h = 0;
index = find( arrayfun(@(x) x.Month==m & x.Day==d & x.Hour==h, s) );
data = s(index).Data;
But the result of arrayfun() is an empty matrix. (I know that the index must be 81.)
What actually works is:
find( arrayfun(@(x) x.Month==m,s) & ...
arrayfun(@(x) x.Day==d,s) & ...
arrayfun(@(x) x.Hour==h,s) )
ans =
81
My question is: why does the first option not work ?
Thank you for an answer.
  댓글 수: 3
dpb
dpb 2014년 6월 5일
편집: dpb 2014년 6월 5일
If it walks like a duck, quacks like a duck, plays golf like the AFLAC duck, ... :)
OKAY, I always start out thinking I've just got a comment then something happens--like the realization of the [] to build the arrays above was after I was already to sign off having simply noted his solution worked for me. I don't follow what didn't in his case, yet, however...
I'd noticed you'd been pinging a bunch but I'm not sure I'll live long enough to get to the privilege level of any significance... :)
The only thing I miss as structured is that sometimes it would be good to re-edit a submission but it takes excessive amount of time already. What I really miss that doesn't seem to exist at all is for feedback from the privileged be considered in updating the forum. It wouldn't be necessary to have to re-edit so much code if the default weren't wordwrap and a few other details like that...there is so much wasted effort in cleaning up happening even as it is and not a tenth of what should gets done.
Image Analyst
Image Analyst 2014년 6월 5일
You're not far from 2000 when you'll be able to edit posts of others. I'll help by giving you 2 points per vote so you'll be there shortly.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2014년 6월 5일
편집: dpb 2014년 6월 5일
Not certain; it did for a small sample case here...but you can write it w/o arrayfun as
ix=find([s.Month]==m & [s.Day]==d & [s.Hour]==h);
making the arrays from the struct.
ADDENDUM
Or, add a datenum value as another field and make your search on the single variable might be faster searching and certainly less complicated to build the search pattern I think. It's certainly much simpler to do ranges that way.
  댓글 수: 2
Alexander
Alexander 2014년 6월 6일
dpb, thanks for the answer.
I had a typing error, so the first mentioned code does it too, as well as yours:
% variant 1
find(arrayfun(@(x) x.Month==M & x.Day==D & x.Hour==H,s))
% variant 2 without arrayfun
find([s.Month]==M & [s.Day]==D & [s.Hour]==H)
But your code is 10 times faster, thus using arrayfun makes really no sense in this case.
Alex
dpb
dpb 2014년 6월 6일
Figured had to be something of the sort; glad you found it.
Interesting that there's that large a speed difference...I hadn't timed it as just used a very short handmade test vector to verify syntax.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by