how to use unique command for the structured array

25 ビュー (過去 30 日間)
meh alipoo
meh alipoo 2016 年 10 月 26 日
編集済み: meh alipoo 2016 年 10 月 26 日
How do I use unique command for a structured array and do the that only based on data of one part of that array. for example:
"a" is a struct array with fields:
z
fit
and
a(1).fit = [5;7]
a(2).fit = [3;8]
a(3).fit = [5;7]
a(1).z = 'Sunday'
a(2).z = 'Monday'
a(3).z = 'Tuesday'
I want to delete the repetitive raw 3, i.e. "a(3).fit = [5;7]" from structure array and its related raw which is "a(3).z = 'Tuesday'" to have :
a(1).fit = [5;7]
a(2).fit = [3;8]
a(1).z = 'Sunday'
a(2).z = 'Monday'
  2 件のコメント
Guillaume
Guillaume 2016 年 10 月 26 日
編集済み: Guillaume 2016 年 10 月 26 日
a.fit(1) = [5;7]
is not valid matlab (you're assigning two elements 5 and 7 to a single element fit(1)). The same is true of a.z(1) = somevector.
Did you actually mean
a(1).fit = [5;7]
a(2).fit = [3;8]
a(3).fit = [5;7]
which would make a a 3x1 or 1x3 struct array with scalar fields?
meh alipoo
meh alipoo 2016 年 10 月 26 日
Thank you for your comment you are true I edit my mistake

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

採用された回答

Guillaume
Guillaume 2016 年 10 月 26 日
編集済み: Guillaume 2016 年 10 月 26 日
Assuming you actually meant a structure such as the one created by:
a = struct('fit', {[5;7], [3;8], [5;7]}, 'z', {'Sunday', 'Monday', 'Tuesday'})
Then the following will work:
[~, idx] = unique([a.fit].', 'rows', 'stable'); %stable optional if you don't care about the order.
a = a(idx)
[a.fit] concatenates the columns in each a.fit to make a single matrix (so all the columns must be the same size). It's then transposed so that unique can work on the rows (formerly columns). The 2nd return value of unique gives you the indices of the rows that were kept, which are the indices of the structure elements to keep.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by