Loop to get mean and max of multiple variable arrays
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2022-3-2
编辑:Stephen23
2022-3-2
"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.
回答(2 个)
Steven Lord
2022-3-2
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 个评论
DGM
2022-3-2
编辑:DGM
2022-3-2
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!