how to remove the repetitive elements from a structure
10 ビュー (過去 30 日間)
古いコメントを表示
>> a = struct('position', {[200 300 1],[300 200 1],[250 250 0.5],[230 280 0.6],[300 200 1],[270 150 1]}, 'cost', {[50;90],[60;80],[65;89],[60;70],[60;80],[55;85]})
I have a structure with two fields, I need to two remove the duplicates?
I tried useing the code below but it did not work.
[~, idx] = unique([a.position].', 'rows', 'stable'); %stable optional if you don't care about the order.
a = a(idx)
I hope I can find a sulotion for this problem.
回答 (1 件)
Stephen23
2019 年 4 月 21 日
編集済み: Stephen23
2019 年 4 月 21 日
Data:
>> a = struct('position', {[200 300 1],[300 200 1],[250 250 0.5],[230 280 0.6],[300 200 1],[270 150 1]}, 'cost', {[50;90],[60;80],[65;89],[60;70],[60;80],[55;85]});
>> a.cost
ans =
50
90
ans =
60
80
ans =
65
89
ans =
60
70
ans =
60
80
ans =
55
85
Then remove duplicates:
N = numel(a);
X = false(1,N);
for ii = 2:N
Y = false;
for jj = 1:ii-1
Y = Y || isequal(a(ii),a(jj));
end
X(ii) = Y;
end
a(X) = []
Giving (fifth element has been removed):
>> a.cost
ans =
50
90
ans =
60
80
ans =
65
89
ans =
60
70
ans =
55
85
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!