How to perform operations on cell arrays?

45 vues (au cours des 30 derniers jours)
Jesse Finnell
Jesse Finnell le 8 Oct 2019
Modifié(e) : Adam Danz le 10 Oct 2019
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
Adam Danz
Adam Danz le 8 Oct 2019
"How to perform operations on cell arrays?"
cellfun()
Jesse Finnell
Jesse Finnell le 9 Oct 2019
I need to have the output of my operation to be cells that are a single column vector whose values are derived from the previous corresponding cell. Like that of above. For example
cumtrapz returns:
X{1}
1
3
5
10
12
16
I need to do an operation that looks like this:
Y{1}
1-0 = 1
3-1 = 2
5-3 = 2
10-5 = 5
12-10 = 2
16-12 = 4
And this is done for X{1} through X{12} and output into Y{1} through Y{12}.

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
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
Jesse Finnell
Jesse Finnell le 10 Oct 2019
Modifié(e) : Jesse Finnell le 10 Oct 2019
Adam Danz I think this line of code from your answer above will work
Vmod = cellfun(@(v){v-[0;v(1:end-1)]},V);
I modified it obviously, to fit my needs. With your % comment I thought differentiation was happening, but when comparing my slow manual process and this result from this I get the same results! I suppose more investigation of the code would have been a wise decision on my end, opposed to continuing to ask questions. Thanks John Doe as well.
Adam Danz
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.
Thanks to John Doe for helping out and following the discussion in close detail.

Connectez-vous pour commenter.

Plus de réponses (1)

John Doe
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
Jesse Finnell
Jesse Finnell le 10 Oct 2019
I get this error when I ran your code
Index in position 1 exceeds array bounds (must not exceed 50).
Error in test (line 4)
deltaV(1,k) = trapz(a(k:k+1,1)) % integral over 1 period
John Doe
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

Connectez-vous pour commenter.

Produits


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by