필터 지우기
필터 지우기

How to add time from 2 arrays

조회 수: 4 (최근 30일)
SS
SS 2014년 10월 16일
답변: Andrei Bobrov 2014년 10월 16일
Hi, I am a new user of Matlab. I have the following problem:
I have two columns like this way.
  • a =102 sec
  • a =135
  • a =157
  • b =189
  • b =201
  • a =222
  • a =245
  • b =290
Here the values are in seconds. I want to write a formula, which show the total duration of seconds of a and b.
For example: here total duration of a is = (157-102) + (245-222) = 78 seconds.
Please suggest me how to do it.
Thanks
  댓글 수: 6
José-Luis
José-Luis 2014년 10월 16일
So is your variable a table or do you have two column vectors: one with strings and the other with numbers?
SS
SS 2014년 10월 16일
It's two column vectors: one with string and other with number.

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 10월 16일
C = {'a' 102
'a' 135
'a' 157
'b' 189
'b' 201
'a' 222
'a' 245
'b' 290}
cn = [C{:,2}]';
[~,~,c1] = unique(C(:,1));
t = [true;diff(c1)~=0];
s1 = accumarray(cumsum(t),cn,[],@(x)x(end)-x(1));
out = accumarray(c1(t),s1);

추가 답변 (1개)

Rainer
Rainer 2014년 10월 16일
I don't see the two columns and I don't understand what a and b are.
  댓글 수: 1
SS
SS 2014년 10월 16일
sorry..i corrected it:
  • a =102 sec
  • a =135
  • a =157
  • b =189
  • b =201
  • a =222
  • a =245
  • b =290

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by