MATLAB Answers

Taking means of initial values in individual cells of structure

조회 수: 1(최근 30일)
Rasmus Herlo
Rasmus Herlo 15 Nov 2019
편집: Daniel M 17 Nov 2019
This is my first question in here, so excuse for lacking syntax or structure:
I have a structure ('tracks') containing many fields (fx 'A'), and in each row there is a cell containing a track of 1000frames.
I would like to estimate the initial values of all traces, but since there are noise, I'd like to pull out the mean value of the three initial values instead of just the initial value.
I have looked around matlab and seems to be needing the right formulation of a cell-function, but neither of my attempts, fx:
Int = cellfun(@mean, {tracks.A(1:3)}, 'UniformOutput', false);
Int2 = cellfun(@(x) mean(x), tracks.A(1:3), 'UniformOutput',false);
seems to be working.
Can anybody tell me what I am missing here, and how to write it correctly, please?
-very much appreciated

  댓글 수: 0

로그인 to comment.

채택된 답변

Daniel M
Daniel M 16 Nov 2019
편집: Daniel M 17 Nov 2019
tracks.A = [{1:6};{10:14}];
Int2 = cellfun(@(x) mean(x(1:3)), tracks.A);
% ans = [2; 11];
If instead you have a non-scalar struct:
tracks(1).A = [1:6];
tracks(2).A = [10:14];
Int2 = cellfun(@(x) mean(x(1:3)), {tracks.A});
% ans = [2; 11];

  댓글 수: 0

로그인 to comment.

추가 답변(1개)

Rasmus Herlo
Rasmus Herlo 17 Nov 2019
Thanks for the effort Daniel. My tracks-structure was slightly different than the one you used, so that tracks(1).A = [1:1000], tracks(2).A = [1001:2000], etc...
Your suggestion therefore gave the error:
Error using cellfun
Input #2 expected to be a cell array, was double instead.
However, it gave the inspiration to put in the (1:3) in the rigth location, as:
Int2 = cellfun(@(x) mean(x(1:3)), {tracks.A}), so thank you
Would it now be good manners to accept your answer, or should I avoid it to prevent confusion?
Thanks

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by