using accumarray to combine text

5 ビュー (過去 30 日間)
Pete sherer
Pete sherer 2022 年 1 月 26 日
回答済み: Stephen23 2022 年 8 月 22 日
Hi,
I am trying to use accumarray to concatenate string.
tdata = table([ 1 2 2 3 4 2]', ["a","b","c","d","e","f"]', 'VariableNames',{'EBID','Name'});
[uniqEBID,~,JGrp] = unique( tdata(:, {'EBID'}));
joinStr = @(tStr) { sprintf([repmat('%s|| ', 1, length( tStr)-1) '%s'], string( tStr))};
joinStr( tdata.Name) % anonymous function seems to do the job
However when running script below, i got error message
accumarray( JGrp, tdata.Name, [], joinStr)
Error using accumarray
Second input VAL must be a full numeric, logical, or char vector or scalar.
  1 件のコメント
Pete sherer
Pete sherer 2022 年 8 月 22 日
the answer should be
"a"
"b|| c|| f"
"d"
"e"

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2022 年 8 月 22 日
T = table([1;2;2;3;4;2], ["a";"b";"c";"d";"e";"f"], 'VariableNames',{'EBID','Name'})
T = 6×2 table
EBID Name ____ ____ 1 "a" 2 "b" 2 "c" 3 "d" 4 "e" 2 "f"
G = findgroups(T.EBID);
F = @(s)join(s,"||");
S = splitapply(F,T.Name,G)
S = 4×1 string array
"a" "b||c||f" "d" "e"

その他の回答 (1 件)

Voss
Voss 2022 年 1 月 26 日
Does this do what you want?
tdata = table([ 1 2 2 3 4 2]', ["a","b","c","d","e","f"]', 'VariableNames',{'EBID','Name'});
[uniqEBID,~,JGrp] = unique( tdata(:, {'EBID'}));
joinStr = @(tStr) { sprintf([repmat('%s|| ', 1, length( tStr)-1) '%s'], string( tStr))};
% joinStr( tdata.Name) % anonymous function seems to do the job
joinStr( tdata.Name(JGrp))
ans = 1×1 cell array
{'a|| b|| b|| c|| d|| b'}

カテゴリ

Help Center および File ExchangeTrigonometry についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by