Search all strings occuring in structure
Show older comments
I have a structure called A (as attached) with the fields Mother, SampleName and Value. Every eight variables SampleNames belong to the same Mother, which is coded in the name (e.g. Mother: 'cutTDM100_111_111_111_111_000' and SampleName: 'cutTDM050_111_111_111_111_122'). I would like to sum up the eight values that each belong to the same Mother. I tried a loop using strcmp, but there is something wrong and it keeps on adding even after the eight value.
sum_value = zeros (3,3)
for k=1:length(A)
while strcmp(A(k+1).Mother,A(k).Mother)
value_add= (A(k).value)
sum_value= sum_value + value_add
end
end
This is probably not the most elegant solution anyways and some index for the output would have to be added. Another thing I could think of, is extracting all the occuring Mother Names and write a loop based on this. However, I could not figure out how to search all the strings occuring withing the structure. Getfield only returns one name.
getfield(A(n), 'Mother')
Accepted Answer
More Answers (2)
If I understood correctly this should do what you want:
mothers = {A.Mother}; %concatenate all Mother fields into a cell array of string
[mother, ~, subs] = unique(mothers); %identify identical mothers
sum_value = accumarray(subs, [A.value]); %sum (default function of accumarray) all values belonging to identical mothers
Note that sum_value is in the same order as mother which is alphabetical. If you prefer them to be in the same order as the values occur in the structure, add the 'stable' option to unique.
Also note that sum_value is the sum of all the values for an identical mother, if you want to restrict it to at most 8 values (and discard the rest?) then:
sum_value = accumarray(subs, [A.value], [], @(v) sum(v(1:min(8, end))));
edited for incorrect order in the arguments of accumarray
4 Comments
Julia
on 25 Mar 2016
Guillaume
on 25 Mar 2016
For some reason, I've swapped first and second argument of accumarray. It should have read
accumarray(subs, [A.value]);
Note that it assumes that each value field is scalar.
Guillaume
on 25 Mar 2016
Since you've confirmed that the value field is not scalar, it's probably simpler to forget about accumarray. You'd still start with unique:
[mothers, ~, subs] = unique({A.Mother});
sum_value = cell(size(mothers));
for mother_idx = 1:numel(mothers)
values = {A(subs == mother_idx)};
sum_value{mother_idx} = sum(cat(3, values{:}), 3);
end
If you only want to sum a maximum of 8 values per unique mothers, then replace the sum line with:
sum_value{mother_idx} = sum(cat(3, values{1:min(end, 8}), 3);
Julia
on 28 Mar 2016
Walter Roberson
on 24 Mar 2016
0 votes
Inside your while loop, you are not changing any of the values you are strcmp()'ing on. Perhaps you just want an "if" instead of a "while".
Categories
Find more on Workspace Variables and MAT Files 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!