How to store corresponding two data to new matrix by using forloop?
Show older comments
Now,i have a matrix, like follows: The 'value' is correspond to the 'time' on the left.
time1 value1 time2 value2
0.82456 0.47 0.78049 0.44
0.84626 0.40 0.80080 0.39
0.86796 0.30 0.82052 0.31
and i want to make a new matrix by using these data. For example, if the 'time' 0.82 or more and less than 0.83, i want to store the corresponding 'value' in the same low(like follows).If there is no corressponding value, it's available to keep the cell as zero or vacant.
time value1 value2
0.78 0.44
0.79
0.80 0.39
0.81
0.82 0.47 0.31
0.83
0.84 0.40
I guess it's better to use for-loops and if statement, but i have no idea. If you some idea to do this, please help me. Thanks.
2 Comments
per isakson
on 22 Aug 2017
Edited: per isakson
on 22 Aug 2017
"If there is no corresponding value" Fine, but if it's more than one value (of value1 or of value2) in an interval, what to do then?
Sayaka Kamata
on 22 Aug 2017
Accepted Answer
More Answers (1)
Akira Agata
on 22 Aug 2017
I would recommend using timetable and retime function, like:
% Convert data to datatable
time1 = [0.82456; 0.84626; 0.86796];
value1 = [0.47; 0.4; 0.3];
time2 = [0.78049; 0.8008; 0.82052];
value2 = [0.44; 0.39; 0.31];
TT1 = timetable(minutes(time1),value1);
TT2 = timetable(minutes(time2),value2);
% Uniform sampling time
time = [minutes(0.78):minutes(0.01):minutes(0.87)]';
% Resample the original data by the uniform sampling time
TT1a = retime(TT1,time,'firstvalue');
TT2a = retime(TT2,time,'firstvalue');
% Output
TTall = [TT1a, TT2a];
The output is as follows:
Time value1 value2
______ ______ ______
0.78 分 NaN 0.44
0.79 分 NaN NaN
0.8 分 NaN 0.39
0.81 分 NaN NaN
0.82 分 0.47 0.31
0.83 分 NaN NaN
0.84 分 0.4 NaN
0.85 分 NaN NaN
0.86 分 0.3 NaN
0.87 分 NaN NaN
2 Comments
per isakson
on 22 Aug 2017
timetable was "Introduced in R2016b"
Sayaka Kamata
on 24 Aug 2017
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!