How to perform operations on cell arrays?
53 views (last 30 days)
Show older comments
I have this section of code
V = cell(1,12);
for k = 1:12
V{k} = cumtrapz(A{k});
end
I now need to convert from a cummulative V to discrete instances of V. It should be noted that columns 1-3, 4-6, 7-9, and 10-12 are of the same size (1=2=3 /= 4, but 4=5=6 etc.). If, for example, A{1,0} = 0, A{1,1} = 1, and A{1,2} = 4, then V{1,1} = 1, and V{1,2} = 3. This operation needs to be performed throughout each cell in order to get velocities at a point instead of a running total.
3 Comments
Accepted Answer
Adam Danz
on 9 Oct 2019
Edited: Adam Danz
on 9 Oct 2019
My understanding of the problem is that you have a cell array of column vectors and you'd like to differentiate them. I've created demo data similar to the data I think you're working with. Please let us know if this doesn't hit the target.
% Create demo data similar to OP's example
X = arrayfun(@(n){unique(randi(30,n,1))},10:15);
% differentiate with leading 0
Y = cellfun(@(x){x - [0; x(1:end-1)]}, X)
% See results of a single element
table(X{1}, Y{1}, 'VariableNames', {'X','Y'})
13 Comments
Adam Danz
on 10 Oct 2019
Edited: Adam Danz
on 10 Oct 2019
Glad to hear you gave it a try! I kept re-reading your description and trying to figure out how it is different from my proposal. I usually give the benefit of the doubt to the OP and assume I'm missing something.
The method I'm using is close to Matlab's diff() function which computes the approximate derivative. The only difference is the leading 0 which preserves the number of elements in the output.
More Answers (1)
John Doe
on 10 Oct 2019
Edited: John Doe
on 10 Oct 2019
I found using trapz is a more intuative method. I used linear acceleration so it was easy to check. I think there is a more efficient way to do this, but I am not certain.
Here is the results.
a = [0:1:49]'; %a = acceleration
deltaV = zeros(1,numel(a))'; % memory pre-allocation
for k = 1:49
deltaV(1,k) = trapz(a(k:k+1,1)) % integral over 1 period
end
figure(1)
plot(a)
xlabel('time')
ylabel('acceleration')
figure(2)
plot(v)
xlabel('time')
ylabel('velocity')
figure(3)
plot(deltaV)
xlabel('time')
ylabel('Change in Velocity')
2 Comments
John Doe
on 10 Oct 2019
Typo on my part in line 3. I've updated.
k = 1:50
Should have been
k = 1:49
See Also
Categories
Find more on Data Type Identification 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!