Search all strings occuring in structure
Mostrar comentarios más antiguos
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')
Respuesta aceptada
Más respuestas (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 comentarios
Julia
el 25 de Mzo. de 2016
Guillaume
el 25 de Mzo. de 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
el 25 de Mzo. de 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
el 28 de Mzo. de 2016
Walter Roberson
el 24 de Mzo. de 2016
0 votos
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".
4 comentarios
Julia
el 24 de Mzo. de 2016
Guillaume
el 24 de Mzo. de 2016
Inside the while loop, k never changes, so if you enter the while loop (because the statement is true) then you'll never exit (since it's always the same true statement that is evaluated)
Julia
el 25 de Mzo. de 2016
Julia
el 28 de Mzo. de 2016
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!