How can I create a matrix or cell array for a changing condition?
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ben Mound
el 27 de Mzo. de 2020
Comentada: Ben Mound
el 28 de Mzo. de 2020
Hi,
I have an irregularly spaced time vector and I would like to create a matrix or cell array with the first column holding all values between 0-6 seconds and the 2nd column 6-12 etc. Im pretty sure there is an easy solution but I am new to matlab and cant find one.
Below is the code I have so far, the result is a matirx which seems to do most of what Im asking but only displaying the first value for each condition.
Any help would be greatly appreciated.
Ben
Cycles = 2
% Vector with irregularly spaced time values
sig_time = [0 0.21 1.83 2.91 2.93 4.04 5.38 5.65 6.89 7.22 7.54 8.62 9.11 9.87 10.02 10.56 11.88 12]'
% Creating Empty Sigma Matrix
Sig_Vec=NaN(length(sig_time),Cycles);
Period = 0;
i=1;
% Seperating Sigma By Cycles
for ii = 1:length(sig_time)
if (Period<=sig_time(ii) && sig_time(ii)<=Period+6)
Sig_Vec(ii,i)=(sig_time(ii));
Period=Period+6;
i=i+1;
end
end
% Result
0 NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN 6.89
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
0 comentarios
Respuesta aceptada
Tommy
el 28 de Mzo. de 2020
Editada: Tommy
el 28 de Mzo. de 2020
Rather than looping through each value in sig_time, loop through the cycles (i.e. array columns):
Period = 0;
for i = 1:Cycles
subset = sig_time(Period <= sig_time & sig_time < Period+6); % all values in sig_time between Period and Period+6
Sig_Vec(1:length(subset),i) = subset;
Period = Period+6;
end
You may prefer to go with the cell array so you don't have to deal with the NaN values:
Sig_Vec = cell(1,Cycles);
Period = 0;
for i = 1:Cycles
Sig_Vec{i} = sig_time(Period <= sig_time & sig_time < Period+6);
Period = Period+6;
end
but of course that means you have to deal with cells.
Más respuestas (0)
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!