Combining duplicate strings in an array

조회 수: 8 (최근 30일)
James Ruffle
James Ruffle 2019년 11월 26일
답변: JESUS DAVID ARIZA ROYETH 2019년 11월 26일
I have a paired string array of 6-digit xyz coordinates which shows the presence of a edge between two nodes.
x = {'010510' '050110' '1'
'020610' '060210' '1'
'010510' '050110' '1'
'050110' '010510' '1'}
I would like to convert this into a weighted edge list using column 1 and 2 data to generate the following:
y = {'010510' '050110' '3'
'020610' '060210' '1'}
Presumably this needs to use unique and accumarray, however it is a requirement to keep the data as a string of 6 digits and not lose 0's at the start...
Grateful for any working solution!

답변 (1개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 11월 26일
solution:
[m,n,l]=unique(join(sort(x(:,1:2)')'));
y= horzcat(split(m),cellstr(num2str(sum(repmat(n',size(x,1),1)==l)')))

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by