How to add time from 2 arrays
1 次查看(过去 30 天)
显示 更早的评论
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
2014-10-16
So is your variable a table or do you have two column vectors: one with strings and the other with numbers?
采纳的回答
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);
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!