How can I concatenate vectors based on a grouping label contained by a separate variable?

조회 수: 1 (최근 30일)
Hello all,
I have 16 test subjects. Each subject underwent a different number of tests. For each test, I have a results vector. I would like to concatenate all the results vectors of the same subject.
Let met illustrate this with psuedo code.
Imagine I have the following test labels as strings, and in a separate variable, I have their test results vectors, each of which may be of different length. The indices of these two seperate variables match up, so that the nth test label corresponds to the nth results vector:
Test labels Results vector
'charlie_test_a' [54 (real) element vector]
'charlie_test_b' [59 (real) element vector]
'charlie_test_c' [23 (real) element vector]
'jane_test_a' [24 (real) element vector]
'jane_test_b' [21 (real) element vector]
'mildred_test_a' [119 (real) element vector]
'mildred_test_b' [ 89 (real) element vector]
'mildred_test_c' [24 (real) element vector]
'pelagius_test_a' [314(real) element vector]
...
What I would like, is to concatenate the results vector of each subject so that I would get something (e.g. a struct concat_results) like
concat_results.charlie = [136 (real) element vector]
concat_results.jane = [45 (real) element vector]
concat_results.mildred = [232 (real) element vector]
Is there an intelligent/fast way to perform this grouped concatenation?
Thank you!

채택된 답변

Stephen23
Stephen23 2021년 6월 4일
편집: Stephen23 2021년 6월 4일
N = ["charlie_test_a";"charlie_test_b";"charlie_test_c";"jane_test_a";"jane_test_b";"mildred_test_a";"mildred_test_b";"mildred_test_c";"pelagius_test_a"]
N = 9×1 string array
"charlie_test_a" "charlie_test_b" "charlie_test_c" "jane_test_a" "jane_test_b" "mildred_test_a" "mildred_test_b" "mildred_test_c" "pelagius_test_a"
D = {rand(1,54);rand(1,59);rand(1,23);rand(1,24);rand(1,21);rand(1,119);rand(1,89);rand(1,24);rand(1,314)};
The old-fashioned way:
[U,X,Y] = unique(extractBefore(N,"_"));
F = @(n)horzcat(D{Y==n});
C = arrayfun(F,1:max(Y),'uni',0);
S = cell2struct(C,U,2)
S = struct with fields:
charlie: [1×136 double] jane: [1×45 double] mildred: [1×232 double] pelagius: [1×314 double]
Or using a table... not sure if this is much better:
T = table(N,D);
T.N = extractBefore(T.N,'_');
T = unstack(T, "D", "N", 'AggregationFunction',@(c){horzcat(c{:})});
T.charlie{1}
ans = 1×136
0.2737 0.3886 0.4304 0.4560 0.1101 0.8067 0.6387 0.3293 0.4217 0.7747 0.9228 0.6502 0.5434 0.1141 0.8645 0.9631 0.4089 0.3211 0.8731 0.3387 0.9358 0.8205 0.3495 0.0055 0.8159 0.0477 0.9314 0.7894 0.1157 0.4364
T.jane{1}
ans = 1×45
0.4863 0.7829 0.1432 0.3304 0.8526 0.4256 0.4426 0.4981 0.1038 0.4425 0.8619 0.7347 0.5812 0.5437 0.5414 0.8597 0.6437 0.7617 0.2114 0.0341 0.7536 0.7526 0.8522 0.2519 0.9126 0.9896 0.6591 0.0517 0.3433 0.6943

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by