Adding elements to array without repeating

Hello,
my question is the following: I have a struct file and I need to add all the elements in the vectors contained in the struct in a single vector without repeating them. My struct is shaped like this:
F.a=[226;227;228;229;290]
F.b=[52;102;230;231;232;233;234;235]
F.c=[37;233;234;235]
and so on. I need to put all the elements of the vectors together but avoiding, for example, to show 233, 234 and 235 twice. Is there a way to do that?
Thank you in advance for your answers!

 採用された回答

Yongjian Feng
Yongjian Feng 2021 年 7 月 8 日

0 投票

This will work:
result = [F.a F.b F.c];
result = unique(result);

その他の回答 (1 件)

Yongjian Feng
Yongjian Feng 2021 年 7 月 7 日

0 投票

Use ismember(233, F.a) before adding it to F.a.

6 件のコメント

Marco Gualtieri
Marco Gualtieri 2021 年 7 月 7 日
Hello Yongjian Feng, thank you for your answer
I would like the code to do it automatically, the actual struct file is much bigger than the one I reported
Yongjian Feng
Yongjian Feng 2021 年 7 月 7 日
Please provide more details. Do you want to merge F.a, F.b, and F.c into a new array, and then get rid of all the duplicated? If so
result = [F.a F.b F.c];
result = unique(result);
Marco Gualtieri
Marco Gualtieri 2021 年 7 月 8 日
Yes, that is what I would like to do. Your idea is the right one, I put it in a loop to make it merge all the substructures of F and now it works. Thank you for your help!
Marco Gualtieri
Marco Gualtieri 2021 年 7 月 8 日
Is there a way to accept your comment as an answer?
Yongjian Feng
Yongjian Feng 2021 年 7 月 8 日
I can put it to an answer, then you can accept it. This will help the other users. Thanks.
Marco Gualtieri
Marco Gualtieri 2021 年 7 月 8 日
You're welcome, thank you for your help!

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

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by