How can I create a matrix or cell array for a changing condition?
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!