Cell array: How can I calculate the mean of it?

Hello,
I got an cell array. You can find it attached.
time1, etc. are the times of the different testing. And values are the meassured values according to the times. So values 1 is connected with time1 and values2 is connected with time2
I would like to get the mean of the "values1" if the conditions "time1>0 & time<=3" is fulfilled.
So the solution would be (4+6+3)/3=4.33333
How can I do this?
Thanks.

3 Comments

Is it a cell vector? The cell is Nx1 and each of the N cell elements contains one of the rows you've shown as a numeric vector?
Make it easy for people to help you, not hard, by attaching your cell array in a .mat file. In the meantime, check out cellfun(), cell2mat(), and mean().
I attached the file. Can you please help me?

Sign in to comment.

 Accepted Answer

load test_file.mat
idx=find(time1>0 & time1<=4);
result=sum(values1(idx))/length(idx);
Do the same for others.

7 Comments

This looks good but does it also work when I search something in between?
For example:
idx=find(time1>1 &time1<=4)
And can u tell me what I could use for the end of the time1? Can I use the "end" command? Or is it enough if i just something like this and matlab will take all values till the end?
idx=find(time>4)
Yes, it gives the index position of the values which satisfy the conditions.
For end element
>> values1(end)
ans =
7
Hope question is answered!
Thanks!
Two more questions:
If i seperate the times and the values into two different cells (for better readability), does it still work?
And how can I work with the cells? And not with the vectors. Your solution seems to be a vector based solution
If i seperate the times and the values into two different cells ??
Sorry, not getting the question exactly. Already they are in separate/different variables. You can do whatever you want.
cell use {}
Suppose
A={[1 2 3 4], [2 3 4 5], [3 0 6], [2 3]}
Now
A{2} result [2 3 4 5]
Creating two cells. 1 cell for the times and 1 cell for the values.
And can u show me a cell based solution for my problem?
Thanks.
I dont know, why you are interested to creating two cells?
Read here cell the differences.
Good Night!

Sign in to comment.

More Answers (1)

Dave
Dave on 30 Jun 2019
Edited: Dave on 30 Jun 2019
Hi Max,
Since it looks like you have the time1 and values1 data in a 1 x 7 numeric array I would create a single 2 x 7 array an then transpose it. It reads better, atleast for me.
A = [time1;values1]';
Take a look at then using find() with the conditions you specified to identify those rows of A that meet the conditions. Apply this index to the 2nd column and then use the mean() function on the output.

1 Comment

Can you write an example? And how could I plot it?

Sign in to comment.

Categories

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!