Clear Filters
Clear Filters

Loop to get mean and max of multiple variable arrays

1 view (last 30 days)
Hi All,
I am still learning how to code and have an issue that I feel isnt too hard but struggling with the concept. Was hoping someone could provide an example so I can see how to do this correctly. Any and all help is greatly apperciated! I have a bunch of variables that are esstentially cloumns of data, 1000 by 1 maxtrix. Lets say they are called car1, car2, car3, car4. I want to write a loop to compute the mean and max for each car (1-4) and then save them as new variables called car1_max, car1_mean, car2_max, car2_mean, and so on. Thanks in advance for the help!
  3 Comments
Kevin Moore
Kevin Moore on 2 Mar 2022
Thank you for the advice. I defiently want to make sure I do things efficiently and dont learn bad habitits so apperciate it. Would you be able to provide an example code for how you would use the indexing to find a max for each column, if you have a ton of rows and columns and the cloumns are all different data. I read through the link just trying to process how to do that. Again thanks for the help!
Stephen23
Stephen23 on 2 Mar 2022
Edited: Stephen23 on 2 Mar 2022
"Would you be able to provide an example code for how you would use the indexing to find a max for each column"
Finding the maximum of columns of a matrix does not require using indexing:
M = rand(5,7)
M = 5×7
0.0746 0.9893 0.0305 0.9127 0.3385 0.8059 0.1889 0.0752 0.1171 0.7823 0.4400 0.7946 0.7802 0.6999 0.7434 0.0201 0.9175 0.5588 0.0748 0.5210 0.6822 0.6393 0.0378 0.7211 0.8420 0.0971 0.4359 0.8149 0.7176 0.4528 0.2745 0.4679 0.3670 0.1013 0.3924
V = max(M,[],1)
V = 1×7
0.7434 0.9893 0.9175 0.9127 0.7946 0.8059 0.8149
Simpler data design -> simpler, much more efficient code.
"I read through the link just trying to process how to do that."
What link are you referring to? I did not give you any links, nor are there any links in your question.

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 2 Mar 2022
Can you define variables with numbered names like car1, car2, car3, ... ? Yes.
Should you do this? Generally we recommend against it. See that page for alternatives you should use instead.

DGM
DGM on 2 Mar 2022
Edited: DGM on 2 Mar 2022
Here's a couple simple examples:
% let's assume all the columns are the same size
% a numeric array should suffice
allcars = rand(100,4); % 4 column vectors for 4 cars
carmeans = mean(allcars,1)
carmeans = 1×4
0.5037 0.4888 0.5407 0.4585
carmax = max(allcars,[],1)
carmax = 1×4
0.9660 0.9920 0.9979 0.9987
% let's assume they aren't all the same size
% in that case, a cell array may work
allcars = {rand(100,1),rand(75,1),rand(50,1),rand(25,1)};
carmeans = cellfun(@mean,allcars)
carmeans = 1×4
0.4904 0.4512 0.4647 0.4974
carmax = cellfun(@max,allcars)
carmax = 1×4
0.9740 0.9831 0.9344 0.9922

Categories

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

Community Treasure Hunt

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

Start Hunting!