Calculate mean of column in structure if another row = 1

2 views (last 30 days)
I have a structure that has columns Correct (either 0 or 1) and Latency. I am attempting to calculate a mean of the Latency values ONLY if correct = 1. So the Latency mean value from the data set below I am hoping to get is 402
Correct Latency
1 325
0 359
1 410
1 425
0 371
1 448
I have attempted something like this:
if struct.Correct == 1
latmean = mean(struct.Latency)
end
But I get the error "Error using == Too many input arguments"
Any thoughts on a way to achieve this? Thanks
  1 Comment
Daniel
Daniel on 13 Sep 2014
As a follow-up question what if I add another array to the cell below
1 0 1 1 0 1
325 359 410 425 371 448
4 4 4 5 5 5
Now is it possible to give an output separately for out=mean(B(2,logical(B(1,:)))) dependent on whether array 3 == 4 or 5?
Thanks

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 12 Sep 2014
v=[ 1 325
0 359
1 410
1 425
0 371
1 448]
A=struct('correct',num2cell(v(:,1)),'latency',num2cell(v(:,2)))
B=cell2mat(struct2cell(A))
out=mean(B(2,logical(B(1,:))))

More Answers (0)

Community Treasure Hunt

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

Start Hunting!