- in your array A, A(1:8) belong to the same number, A(9:16) to another mother, etc..
- There are N mothers and, hence, N*8 elements in A.
- A(k).Value is a scalar
Search all strings occuring in structure
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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')
0 Commenti
Risposta accettata
Jos (10584)
il 25 Mar 2016
So, I assume:
AllValues = [A.Value]
AllValues = reshape(AllValues,8,[]) % a 8-by-N array
SumValue = sum(AllValues,1) % sum each row
Note that this is not very flexible ...
3 Commenti
Jos (10584)
il 25 Mar 2016
Aha, we can concatenate and sum in higher dimensions! This might then work:
AllValues = cat(3,A.Value) % a 3x3x(N*8) array
AllValues = reshape(AllValues,3,3,[],8) % a 3-3-N-by-8 array
SumValue = sum(AllValues,4) % a 3x3xN matrix
It could be that I switch the N and the 8, so you have to check!
Più risposte (2)
Guillaume
il 24 Mar 2016
Modificato: Guillaume
il 25 Mar 2016
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 Commenti
Guillaume
il 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);
Walter Roberson
il 24 Mar 2016
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 Commenti
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!