I have a huge cell array. Column 1 has duplicate material numbers. Column 3, 4 contains numbers that I want to add. Column 2 contain either texts or alphanumerals which I just want to retain.
'Material Number' 'Material Description' 'Quantity withdrawn' 'Value Withdrawn'
107552 Material A 5 $40
18042 Material 5B 1 $90
107552 Material A 1 $8
Desired Output:
'Material Number' 'Material Description' 'Quantity withdrawn' 'Value Withdrawn'
107552 Material A 6 $48
18042 Material 5B 1 $90
I'm not sure how to do this in case of celltype. I tried with just number (double) and it worked.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 16일

0 개 추천

Try this example
C = {107552, 'Material A', 5, 40; ...
18042, 'Material 5B', 1, 90; ...
107552, 'Material A', 1, 8};
ids = [C{:,1}].';
result = splitapply(@(x) {[x(1,1:2) sum([x{:,3}]) sum([x{:,4}])]}, C, findgroups(ids));
result = vertcat(result{:});
Result
>> result
result =
2×4 cell array
{[ 18042]} {'Material 5B'} {[1]} {[90]}
{[107552]} {'Material A' } {[6]} {[48]}

댓글 수: 2

Thanks a lot. It works. I just need to study more about splitapply to use to elsewhere.
Ameer Hamza
Ameer Hamza 2020년 5월 16일
I am glad to be of help!
Yes, splitapply can be a very useful tool if applied correctly.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by