How to perform operations on cell arrays?
48 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Adam Danz
le 9 Oct 2019
Modifié(e) : Adam Danz
le 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 commentaires
Adam Danz
le 10 Oct 2019
Modifié(e) : Adam Danz
le 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.
Plus de réponses (1)
John Doe
le 10 Oct 2019
Modifié(e) : John Doe
le 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 commentaires
John Doe
le 10 Oct 2019
Typo on my part in line 3. I've updated.
k = 1:50
Should have been
k = 1:49
Voir également
Catégories
En savoir plus sur Data Type Identification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!