필터 지우기
필터 지우기

Summing the elements of cell arrays for only certain values

조회 수: 2 (최근 30일)
Daniel
Daniel 2014년 4월 2일
답변: Jos (10584) 2014년 4월 3일
Does anyone know an efficient way to sum up the last value in a unique instances of a cell array of strings?
For instance
A = {'a', 'b', 'c', 2
'a', 'b', 'd', 3
'b', 'c', 'd', 7
'a', 'b', 'd', 10};
the answer that I would return would be every unique combination so I would return a cell array with three unique strings, with values of 2, 7, and 13.

채택된 답변

Jos (10584)
Jos (10584) 2014년 4월 3일
[~,~,j] = unique(A(:,1:3)) ;
[~,x,k] = unique(reshape(j,[],3),'rows')
S = accumarray(k,[A{:,4}])
out = [A(x,1:3) num2cell(S)]

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 2일
A = {'a', 'b', 'c', 2
'a', 'b', 'd', 3
'b', 'c', 'd', 7
'a', 'b', 'd', 10}
c=A(:,1:3)
d=arrayfun(@(x) strjoin(c(x,:)),(1:size(c,1))','un',0)
[qq,idx,kk]=unique(d,'rows','stable')
f=accumarray(kk,cell2mat(A(:,4)))
out=[c(idx,:) num2cell(f)]

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by