Hi, I need help in writing a code formula
1 view (last 30 days)
Show older comments
I'm writing a series of code which count total of activations, how long activated and time to next activation. I have so far managed the Start points and end points but i'm not sure how to go about get time between the points and time to start of next activation in time increments.
ATsyt2= T.ATSys.signals.values==2; % Channel
FATsyt2=find(ATsyt2==1); % Determine when condition is met
starATsyt2=FATsyt2([0; diff(FATsyt2)]>1); %Start of Activation
endFATsyt2=(starATsyt2-1);
endFATsyt2=endFATsyt2(2:end); % End of Activation
6 Comments
Walter Roberson
on 24 Mar 2019
Is an activation a sequence of consecutive entries in which the signal value is exactly 2 ?
Accepted Answer
Walter Roberson
on 24 Mar 2019
dT = 0.1;
mask = (T.ATSys.signals.values(:)==2).'; %row vector
starts = strfind([false mask], [false true]);
stops = strfind([mask false], [true false]);
activation_durations = dT * (stops - starts + 1);
%an activation of 1 sample would have equal stop and start so difference would be 0, but that should be considered 1 timestep
activation_times = arrayfun(@(start,stop) (start:stop)*dT, starts, stops, 'uniform', 0); %this assumes first time is dT not 0
Now, activation_durations is a vector of durations (minimum dT for an activation of one point duration), and activation_times is a cell array of the timestamps of activations, if you need that for some reason (for example you might need it if you were doing curve fitting.)
4 Comments
Walter Roberson
on 27 Mar 2019
find(mask) * dT
or possibly
(find(mask)-1) * dT %if the first time is to be 0
Or if you have already calculated that cell array that I show, then
[activation_times{:}]
It depends what you need to do with them. It is not uncommon to need to know the times broken out by section rather than all in one array.
Like if you had another array, such as T.ATSys.signals.intensity with one entry for each corresponding .values entry, and you wanted to fit a quadratic to each distinct block, then
intens = T.ATSys.signals.intensity;
activation_intensities = arrayfun(@(ATsyt2starts,ATsyt2stops) intens(ATsyt2starts:ATsy2stops), ATsyt2starts, ATsyt2stops, 'uniform', 0);
cellfun(@(T,Intens) polyfit(T, Intens, 2), activation_times, activation_intensities, 'uniform', 0)
and that would be a per-block fit. Where-as if you had all of the entries together, like
intens = T.ATSys.signals.intensity;
activation_times = find(mask)*dT;
activation_intensities = intens(mask);
polyfit(activation_times, activation_intensities, 2)
would give you a single quadratic fit for the whole thing -- which could be valid if it all represented a single process, whereas the block-by-block fit would be appropriate if each block represented a (semi-) independent process.
I note, by the way, that you changed the variable names in the arrayfun. I had
arrayfun(@(start,stop) (start:stop)*dT, starts, stops, 'uniform', 0)
Notice that the names used as the parameters in the anonymous function are not the same as the names of the variables being processed. Each time the anonymous function fires, it is working on one particular start + stop pair, singular, while starts and stops are plural, collections (vectors) of the data. The plural or not is not the important part, but it is important that the variable names for the anonymous function, used to hold one particular value, are not the same as the variables being acted on, the ones being used to hold the collections of values. When you adjusted the names to what you did, you used the same variable names for the particular as you did for the collections.
It is not that MATLAB will malfunction or produce the wrong answer for what you did.. the problem is that humans will produce the wrong answer when asked to examine what you wrote. When humans read your code, they are likely to get confused about whether the variables inside the anonymous function refer to particular values or the collections.
To avoid this kind of confusion, I use informal (and not rigorous) naming conventions in my code, such as lower case variables for scalars or vectors, and upper case for arrays, such as
[X, Y] = ndgrid(x, y)
and I tend to use names like
arrayfun(@(this_x, this_y) this_x.^2 + this_y, X, Y)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!