Adding elements from a stuck to an array

7 ビュー (過去 30 日間)
Konstantinos Moulakis
Konstantinos Moulakis 2021 年 10 月 8 日
Hello !
I have a struct lets say with 2 list one has some values and the other characterize this values i.e some values belong to group1 some to group 2 etc. the values that are in the 1st group correspond to the number 2 in the second list. I want to take the values from the 1st list that belong lets say to group 2 and add them to an array WITHOUT a for loop.
Can this achieved ?
I manage to do it with a for loop and a if but i had to speed up the script.
Thank you for your time !

採用された回答

Dave B
Dave B 2021 年 10 月 8 日
This might be easier to answer if you provided some example code, as it's a little difficult to follow your description. The ideal would be if you could produce a simple struct that with a few values that demonstrated your problem.
Here's my best guess based on your description:
mystruct=struct('vals', [1 2 3 4 5], 'cats', [1 2 1 2 1])
mystruct = struct with fields:
vals: [1 2 3 4 5] cats: [1 2 1 2 1]
group1 = mystruct.vals(mystruct.cats==1)
group1 = 1×3
1 3 5
group2 = mystruct.vals(mystruct.cats==2)
group2 = 1×2
2 4
  1 件のコメント
Konstantinos Moulakis
Konstantinos Moulakis 2021 年 10 月 11 日
That helped thank you !

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by