How to store corresponding two data to new matrix by using forloop?

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 comentarios

per isakson
per isakson el 22 de Ag. de 2017
Editada: per isakson el 22 de Ag. de 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?
Finally, i want to calculate the average of value like follows.
this table is example, actually, i have more than 40 samples (value1, value2,......value40...)
time value1 value2 ... average
0.78 0.44 ... 0.44
0.79 ...
0.80 0.39 ... 0.39
0.81 ...
0.82 0.47 0.31 ... 0.39
0.83 ...
0.84 0.40 ... 0.40

Iniciar sesión para comentar.

 Respuesta aceptada

data = [ 0.82456 0.47 0.78049 0.44
0.84626 0.40 0.80080 0.39
0.86796 0.30 0.82052 0.31];
time = [data(:,1:2:4)] ;
val = [data(:,2:2:4)] ;
ti = [ 0.78
0.79
0.80
0.81
0.82
0.83
0.84 ] ;
%%fix time to two decimals
time = round(time.* 10^2) ./ 10^2 ;
iwant = NaN(size(ti,1),2) ;
[ia,ib] = ismember(time,ti) ;
for i = 1:size(time,2)
iwant(ib(ib(:,i)~=0,i),i) = val(ia(:,i),i) ;
end

2 comentarios

"if the 'time' 0.82 or more and less than 0.83" asks for floor rather than round
Thanks a lot! it solved!

Iniciar sesión para comentar.

Más respuestas (1)

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 comentarios

timetable was "Introduced in R2016b"
Thanks for your answer, it was helpful and seems to be very simple, but my MATLAB is not the up-to-date type,,, i wasn't able to use the code 'timetable'.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Ag. de 2017

Comentada:

el 24 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!