Clear Filters
Clear Filters

Search all strings occuring in structure

4 views (last 30 days)
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

Jos (10584)
Jos (10584) on 25 Mar 2016
So, I assume:
  • 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
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 Comments
Jos (10584)
Jos (10584) on 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!
Julia
Julia on 28 Mar 2016
I switched N and 8 and now it works. Combined with the input of Guillaume this is sorted now, looking the following. Thanks guys.
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,3) % a 3x3xN matrix
[mother, ~, subs] = unique({A.Mother}); %identify identical mothers
for n=1:(length(mother))
family(n).mother = mother(1,n)
end
for n=1:(length(mother))
family(n).sum_50s_Daughters = SumValue(:,:,1,n)
end

Sign in to comment.

More Answers (2)

Guillaume
Guillaume on 24 Mar 2016
Edited: Guillaume on 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 Comments
Guillaume
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
Julia on 28 Mar 2016
Neither accumarray nor sum seem to work for the 3x3 matrix
Undefined function 'sum' for input arguments of type 'struct'.

Sign in to comment.


Walter Roberson
Walter Roberson on 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 Comments
Julia
Julia on 25 Mar 2016
I have rewritten the loop based on the list of unique mother names. Now it is jumping the 9th value, since the condition is false, and keeps on going on with the 10th value. What do I need to change so I add up values 1-8, 9-16 and so on? I am doing it the complicated way since I don't know how to use accumarray for the 3x3 matrix, see above. Thanks
%build structure
for n=1:(length(motherlist))
family(n).mother = motherlist(n,1)
end
l=1
sum_value=zeros(3,3)
for k=1:length(A)
if strcmp(A(k).Mother,family(l).mother)
value_add= (A(k).value)
sum_value = sum_value + value_add
else
family(l).sum_50s_Daughters = sum_value
l=l+1
value_add=zeros(3,3)
sum_value=zeros(3,3)
end
end
Julia
Julia on 28 Mar 2016
I would still like to know, how to deal with a case like this. Does the if condition have to be outside the for-loop? How do I prevent jumping values?

Sign in to comment.

Categories

Find more on Characters and Strings 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!