フィルターのクリア

Seeking Help: Deleting Redundancy in a Structure Array

2 ビュー (過去 30 日間)
Wei Wen
Wei Wen 2024 年 1 月 24 日
コメント済み: Bruno Luong 2024 年 1 月 24 日
Hello,
I hope this message finds you well. I have a question regarding comparing elements in a structure array in MATLAB. Below is the code snippet I am working with, and I'm looking for guidance on identifying and removing redundancy
clear
clc
a(1).pos=[1,7,1];
a(2).pos=[1,7,1];
a(3).pos=[6,5,5];
a(4).pos=[1,4,3];
a(5).pos=[2,2,2];
In this example, I want to find and delete redundant elements within the structure array. Specifically, if I know that a(1).pos and a(2).pos have the same elements, I want to retain only one instance.
I am seeking assistance on how to code this efficiently. Any insights or code snippets would be greatly appreciated. Thank you in advance for your help!
  2 件のコメント
Bruno Luong
Bruno Luong 2024 年 1 月 24 日
Can we assume
a(i).pos
are 1 x 3 numercal array or they can be something else more generic?
Wei Wen
Wei Wen 2024 年 1 月 24 日
編集済み: Wei Wen 2024 年 1 月 24 日
hi, it is in numerical array.
Here is a sample of the actual value from the main code. I would like to compare the value from the pop.Position. If there is redundancy, i would like to eliminate it. Are there any ways to do that?
There is no redundancy in the figure below.

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

採用された回答

Bruno Luong
Bruno Luong 2024 年 1 月 24 日
a(1).pos=[1,7,1];
a(2).pos=[1,7,1];
a(3).pos=[6,5,5];
a(4).pos=[1,4,3];
a(5).pos=[2,2,2];
P = cat(1, a.pos);
[~, I] = unique(P, 'rows');
a = a(sort(I));
a.pos
ans = 1×3
1 7 1
ans = 1×3
6 5 5
ans = 1×3
1 4 3
ans = 1×3
2 2 2
  2 件のコメント
Wei Wen
Wei Wen 2024 年 1 月 24 日
Thank you !
Bruno Luong
Bruno Luong 2024 年 1 月 24 日
@Wei Wen Can you please accept the answer?

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by