How to perform operations on cell arrays?

49 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2019-10-8
"How to perform operations on cell arrays?"
cellfun()
Jesse Finnell
Jesse Finnell 2019-10-9
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}.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-10-9
编辑:Adam Danz 2019-10-9
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 个评论
Jesse Finnell
Jesse Finnell 2019-10-10
编辑:Jesse Finnell 2019-10-10
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 2019-10-10
编辑:Adam Danz 2019-10-10
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.

请先登录,再进行评论。

更多回答(1 个)

John Doe
John Doe 2019-10-10
编辑:John Doe 2019-10-10
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 个评论
Jesse Finnell
Jesse Finnell 2019-10-10
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 2019-10-10
Typo on my part in line 3. I've updated.
k = 1:50
Should have been
k = 1:49

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by