Find indices in structure array for structures with field meeting a condition

53 ビュー (過去 30 日間)
Ali Komai
Ali Komai 2018 年 10 月 6 日
コメント済み: Ali Komai 2018 年 10 月 7 日
Let's say I have an array of structures. Now I want to find the indices of the structures in the array for which two fields meet a certain condition. Is there a way to accomplish this without looping through the whole structure array? If I have vector A of numbers and simply want all indices for the numbers that are larger then 10 I could write: indices = A > 10; Something in this style just for structure - arrays is what I'm looking for.

採用された回答

Stephen23
Stephen23 2018 年 10 月 7 日
If the data in the fields can be concatenated together (i.e. have the same number of rows/columns/...) then you could use a comma-separated list to create one array, and use that array. For example:
>> S(1).a = 1;
>> S(2).a = 2;
>> S(3).a = 3;
>> idx = [S.a]>2
idx =
0 0 1
>> S(idx).a
ans = 3
Clearly you can do that for any fields. If the data cannot be concatenated together, or have sizes that would be ambiguous when joined together, then you will have to use a loop, even if implicitly inside cellfun. For example:
>> cellfun(@(v)any(v(:)>2),{S.a})
ans =
0 0 1
Read more here:
  1 件のコメント
Ali Komai
Ali Komai 2018 年 10 月 7 日
Thank you so much! This was what I was looking for.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by