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 Kommentare

What does
a 102 a 135 a 157 b 189 b 201 a 222 a 245 b 290
mean?
SS
SS am 16 Okt. 2014
Bearbeitet: SS am 16 Okt. 2014
  • a =102 sec
  • a =135
  • a =157
  • b =189
  • b =201
  • a =222
  • a =245
  • b =290
José-Luis
José-Luis am 16 Okt. 2014
I still don't see the two columns.
SS
SS am 16 Okt. 2014
its actually two columns..I just put an = between them.Here, a is in one column and 102 in other coloumn
José-Luis
José-Luis am 16 Okt. 2014
So is your variable a table or do you have two column vectors: one with strings and the other with numbers?
SS
SS am 16 Okt. 2014
It's two column vectors: one with string and other with number.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 16 Okt. 2014

0 Stimmen

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);

Weitere Antworten (1)

Rainer
Rainer am 16 Okt. 2014

0 Stimmen

I don't see the two columns and I don't understand what a and b are.

1 Kommentar

SS
SS am 16 Okt. 2014
sorry..i corrected it:
  • a =102 sec
  • a =135
  • a =157
  • b =189
  • b =201
  • a =222
  • a =245
  • b =290

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by