Search all strings occuring in structure

조회 수: 3 (최근 30일)
Julia
Julia 2016년 3월 24일
댓글: Julia 2016년 3월 28일
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')

채택된 답변

Jos (10584)
Jos (10584) 2016년 3월 25일
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
Jos (10584)
Jos (10584) 2016년 3월 25일
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 2016년 3월 28일
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

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2016년 3월 24일
편집: Guillaume 2016년 3월 25일
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
Guillaume
Guillaume 2016년 3월 25일
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 2016년 3월 28일
Neither accumarray nor sum seem to work for the 3x3 matrix
Undefined function 'sum' for input arguments of type 'struct'.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2016년 3월 24일
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
Julia
Julia 2016년 3월 25일
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 2016년 3월 28일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by