Adding data

Hello,
I have data in the following format:
00 08 255 267 298 300 - time[s]
5 4 3 4 3 2 - values
Time is given in seconds. The values in between stay the same, by this I mean that from 0s to 08s the value is 5 and then it changes to 4. I'd like to create a table (time vs. values) where each second has assigned a value. Something like this:
00 01 02 03 04 05 06 07 08 09 .....
5 5 5 5 5 5 5 5 4 4 .....
Any help is appreciated.
Thanks in advance

 采纳的回答

Jan
Jan 2011-11-11
t = [0, 8, 255, 267, 298, 300];
x = [5, 4, 3, 4, 3, 2];
index = zeros(1, t(end) + 1);
index(t + 1) = 1;
index = cumsum(index);
y = x(index);

3 个评论

John
John 2011-11-11
great stuff,
thanks very much for that.
One more question if possible...How to print it into a simple table format( 1st row-time, 2nd row values)
fprintf('%10g ', index);
fprintf('\n');
fprintf('%10g ', y);
fprintf('\n');
John
John 2011-11-11
thank you for answering...
but it gives me
1 1 1 1 1 1 1 1 2 2....
5 5 5 5 5 5 5 5 4 4....
instead of
0 1 2 3 4 5 6 7 8 9 .....
5 5 5 5 5 5 5 5 4 4 .....
any suggestion?
thanks

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by