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

 Accepted Answer

Jan
Jan on 11 Nov 2011
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 Comments

John
John on 11 Nov 2011
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)
Walter Roberson
Walter Roberson on 11 Nov 2011
fprintf('%10g ', index);
fprintf('\n');
fprintf('%10g ', y);
fprintf('\n');
John
John on 11 Nov 2011
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

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 11 Nov 2011

Community Treasure Hunt

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

Start Hunting!