Calculating an average if specific condition is fulfilled

Hello everybody,
I am working on my own project right now and needing some help now.
What do I want to do?
Matlab should calculate the average out of values for a specific time period.
time (sec) value
0 2
1 3
2 4
3 6
4 7
5 8
6 2
If time is >0 and smaller or equal to 3 sec Matlab should calculate the arithmetic average of the values.
So basically something like this:
IF 0>=time>=3
Do arithmetic average of value
So the result should be (2+3+4+6 )/4 =3,75.
It is my first project with MatLab and I am usually working with other programms.
Thanks for your help.
Your sincerely,
Max

Answers (1)

madhan ravi
madhan ravi on 6 May 2019
Edited: madhan ravi on 6 May 2019
https://in.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html - read the topic logical indexing once you understand that use mean() after that process.
I also suggest you to do MATLAB on-ramp course.

6 Comments

Thank you for your reply!
I got 10 data sets. For each testing I got times and values.
Can I do it like this:
time1 = 0 1 2 3 4 5 6
values1 = 0 1 2 3 4 5 6
time2 = 0 1 2 3 4 5 6 7 8
values2= 0 1 2 3 4 5 6 7 8
>>mean(values1(0<time1<3)
>>mean(values2(0<time2<4)
And so on?
And can you recommend me tutorial for beginners? So that I can work through the programm?
Thank you again.
https://matlabacademy.mathworks.com - do this course, only takes a couple of hours (so that you don't have to depend on anyone often).
Don't name your variables dynamically see https://in.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval to know why. Use a cell array for intance:
time=cell(2,1);
time{1}=...
time{2}=...
Thanks for your answer :)
so for my project it would be like this?
time=cell(10,1)
Do I need to change the second number to the amount of time values? Or does Matlab do it automatically?
I will start with the course and if I have some questions I will come back and ask theme here :)
I have one more question...
What should I do if the amout of values are NOT the same?
time1= 1 2 3 END
time 2 = 1 2 3 4 5 END
values1= 4 5 6 END
values2 1 2 4 5 6 END
Should I still put them into one matrix?
time=cell(2,1);
time{1} =[ 1 2 3 ]
time{2} =[ 1 2 3 4 5 ]

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Asked:

on 6 May 2019

Commented:

on 9 May 2019

Community Treasure Hunt

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

Start Hunting!