how to calculate a average of five arrays?
16 次查看(过去 30 天)
显示 更早的评论
I have a small problem in a exercise. So i have to make dynamic vectors with 3 dimentions and calculate the average of the sum of all vector's, how can I do this? example:
if true
V = [];
for i=1:5
V1 = [v1 v2 v3]
V2 = [v1 v2 v3]
V3 = [v1 v2 v3]
V4 = [v1 v2 v3]
V5 = [v1 v2 v3]
end
average = [v1 v2 v3]
end
2 个评论
James Tursa
2016-8-19
It is not clear what is being averaged. Your loop doesn't even have anything in it that depends on the index variable i. Can you provide a short example showing inputs and desired output?
回答(2 个)
Azzi Abdelmalek
2016-8-19
If you want to calculate the mean of any matrix, you can use mean function
mean(A)
Image Analyst
2016-8-20
The way you've written it, you must have defined lower case v1, v2, and v3 in advance. You have not given us this code for some reason.
And in your loop, there is no difference between upper case V1, V2, V3, V4, and V5. V1 is the same as V2 and they are the same as V3, etc.
Of course you know that upper case V1 is different than lower case v1, but nevertheless it's bad programming practice to use variable names for two different variables that are so close to the same. One typo and you're in deep trouble without knowing it.
And finally, on each iteration (which you incorrectly called "interaction" in your comment to Azzi), there is no change. So the V1 on iteration 5 will be the very same V1 you got after iteration 1. You could have a thousand iterations and still nothing would change. So what's the point of that? Why even have iterations when nothing changes and you're just doing the exact same thing 5 times?
Other than that, once you figure out some array that you want to take the mean of, you can get the mean of the whole thing, or of just rows or columns, like this:
wholeMean = mean(yourMatrix(:));
rowMeans = mean(yourMatrix, 2); % Means of each row, going across columns.
columnMeans = mean(yourMatrix, 1); % Means of each column, going down rows.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!