Loop to get mean and max of multiple variable arrays
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 2 Mär. 2022
Bearbeitet: Stephen23
am 2 Mär. 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)
V = max(M,[],1)
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.
Antworten (2)
Steven Lord
am 2 Mär. 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.
0 Kommentare
DGM
am 2 Mär. 2022
Bearbeitet: DGM
am 2 Mär. 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)
carmax = max(allcars,[],1)
% 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)
carmax = cellfun(@max,allcars)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!