Loop to get mean and max of multiple variable arrays
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 2 de Mzo. de 2022
Editada: Stephen23
el 2 de Mzo. de 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.
Respuestas (2)
Steven Lord
el 2 de Mzo. de 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 comentarios
DGM
el 2 de Mzo. de 2022
Editada: DGM
el 2 de Mzo. de 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 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!