Sum and Sort Values from Multiple Vectors

조회 수: 2 (최근 30일)
Patrick Rungrugeecharoen
Patrick Rungrugeecharoen 2019년 5월 26일
댓글: Patrick Rungrugeecharoen 2019년 5월 26일
I have a numerical vector:
Time = [10 23 54 50]
This corresponds to the string vector:
Place = [A B C A]
And another string vector:
Unique = [A B C]
How do I count these so that I get the following result:
x = [60 23 54]
This is just a snippet of the actual data - the full data set is at least 5000 long. I figured an if loop would work but I'm having trouble visualising and writing the code out.
Any help would be much appreciated. Thanks :)
  댓글 수: 2
per isakson
per isakson 2019년 5월 26일
편집: per isakson 2019년 5월 26일
A B C are they names of variables or what?
Patrick Rungrugeecharoen
Patrick Rungrugeecharoen 2019년 5월 26일
They're variables yes in the form of strings.

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

채택된 답변

KSSV
KSSV 2019년 5월 26일
Time = [10 23 54 50]
place = {'A' 'B' 'C' 'A'}
place_unique = unique(place)
N = length(place_unique) ;
iwant = zeros(N,1) ;
for i = 1:N
iwant(i) = sum(Time(strcmp(place,place_unique{i}))) ;
end
iwant

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by