Reputation: 3616
I have a struct S
of size 3x1
where S(1,1)=[209,443,319, 206]
S(2,1)=[300,473,1,1]
S(3,1)=[305,470,1,1]
what I want to do is to keep only 1 internal struct where S
will be of size 1x1
instead of nx1
this struct is the one that has the highest values in the 3rd and 4th column among the other structs. So in my example S
will only have S(1,1)
because its 3rd and 4th column are the highest among S(2,1) and S(3,1) with values 319,206 compared to 1,1 of S(2,1)
and S(3,1)
so S(2,1)
and S(3,1)
will be deleted and S
will be 1x1
struct only having S(1,1)
. So if anyone could please advise
Upvotes: 1
Views: 51
Reputation: 238329
I will assume that your measure of that fact that the 3rd and 4th column are bigger then in other arrays is based on their sum.
S = struct();
S(1,1).v=[209, 443, 319, 206];
S(2,1).v=[300, 473, 1, 1];
S(3,1).v=[305, 470, 1, 1];
% find index with maxium 3th and 4th column based on their sum
[~, ind] = max(cellfun(@(v) v(3)+v(4), {S(:).v}))
% set S to be equal only to the value with highest 3th and 4th column
S = S(ind);
Btw, syntax S(1,1)=[209,443,319, 206]
is incorrect. Can't make structure like this. Thus I added a v
field to structure.
In case you mean actually a cell
rather than a struct
as @chappjc indicated, than you can do as follows:
S = {};
S{1,1}=[209,443,319, 206];
S{2,1}=[300,473,1,1];
S{3,1}=[305,470,1,1];
[~, ind] = max(cellfun(@(v) v(3)+v(4), S));
S = S(ind);
Upvotes: 2