필터 지우기
필터 지우기

How to perform operations on cell arrays?

조회 수: 32 (최근 30일)
Jesse Finnell
Jesse Finnell 2019년 10월 8일
편집: Adam Danz 2019년 10월 10일
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에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by