3

I have a fairly simple issue and I just want to know if there's an easy way to do it in MATLAB (i.e. a function to do this rather than writing out loops or something myself).

Let's say I have a timeseries where Time is 1:1:1000 and Data is 2 * (1:1:1000) and I want to expand the array by making the Time and Data vector finer. Let's say that I want Time to be 1:0.1:1000 and Data to be 2 * (1:0.1:1000). Is there an easy way to tell MATLAB that to repeat the values of each vector 10 times (from 1 / 0.1 = 10) so that I can have something like this?:

Time: [1, 2, 3, 4, ...]

Data: [2, 4, 6, 8, ...]

to:

Time: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, ...]

Data: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, ...]

celestialorb
  • 1,901
  • 7
  • 29
  • 50
  • You can use `repmat(Data, 10,1)(:)` for `Data`, and if you know your time series is spaced evenly, you can regenerate it as you mentioned – nicolas Aug 18 '14 at 17:55
  • You might need to check this http://stackoverflow.com/questions/23963605/expand-matrix-in-matlab – CroCo Aug 18 '14 at 18:44
  • 1
    Almost the same question: http://stackoverflow.com/q/1947889/2586922 – Luis Mendo Aug 18 '14 at 21:38

4 Answers4

2

You can use combination of reshape() and repmat() as follow:

Data = [2, 4, 6, 8, ...] % As stated in the question. 
Data = reshape(repmat(Data, 10, 1), 1, []);

This is more time-efficient than the others like kron() or combination of sort() and repmat().

Two simulations were done and the results are shown in the following figures.

First: Simulation time vs. length of Data. Here I used N=100 instead of 10.

enter image description here

Second: Simulation time vs. repetition factor. Length of Data is 10000.

enter image description here

So you can select the best one according to the simulation results.

1

As seb proposed, you can use the function repmat. Here what I would do:

Data = [2, 4, 6, 8, ...];
Data = sort(repmat(Data,1,10));
m_power
  • 3,156
  • 5
  • 33
  • 54
  • How about expanding it out to a data matrix rather than a vector if I wanted to repeat it via rows? – celestialorb Aug 18 '14 at 18:08
  • @celestialorb Do you mean as in `Data = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2; ... 4, 4, 4, 4, 4, 4, 4, 4, 4, 4; ...`?? – m_power Aug 18 '14 at 18:12
  • Let's say I had position data that corresponded to each time instance. Such as `data = [1 2 3; 4 5 6]` for `time = [1 2 3]` and I want to expand that to `data = [1 2 3; 1 2 3; 1 2 3; 4 5 6; 4 5 6; 4 5 6]` and `time = [1 1 1 2 2 2 3 3 3]`. Basically a way to just repeat the rows of data and elements of time by some integer X (X = 3 in the case given in this comment). – celestialorb Aug 18 '14 at 18:19
  • 1
    You would use `repmat` again and specify how many tiles of the matrix you would want repeated. – rayryeng Aug 18 '14 at 18:26
  • @m_power: your approach assumes monotonic increasing data -> not quite generic solution. Use `Data = repmat(Data,1,10); Data = Data(:);` instead. It's faster also. – Robert Seifert Aug 18 '14 at 18:46
  • I am not sure where the `sort` is coming from, but it's not needed. – nicolas Aug 18 '14 at 19:00
  • @thewaywewalk I assumed from the OP question that the data is increasing. – m_power Aug 18 '14 at 19:46
  • @seb: Doing `repmat(Data,1,10)(:)` throws an error. Also, using `sort`, even if slower, makes the code cleaner and more understandable. – m_power Aug 18 '14 at 19:48
1

You can use repmat

interval_size = 10;
Data = 2*(1:1:1000);

out_data = repmat(Data,interval_size,1);
out_data = out_data(:)';
Nishant
  • 2,571
  • 1
  • 17
  • 29
0

Example Data:

time=1:50
data=2:2:100
t2=1:.1:50.9

For time=1:n this is very simple:

data(:,floor(t2))

If your original data has another time scale, use this:

[a,b]=ismember(floor(t2),time)
data(:,b)
Daniel
  • 36,610
  • 3
  • 36
  • 69