how to remove the repetitive elements from a structure
Show older comments
>> 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.
Answers (1)
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
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!