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
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
Azzi Abdelmalek 2016-8-19
If you want to calculate the mean of any matrix, you can use mean function
mean(A)
  1 个评论
Biza Ferreira
Biza Ferreira 2016-8-19
yes, is not clear. I want construct a dynamic vector where each vector have 3 dimensions, after five interactions, where the result are 5 vectors (V1,V2,V3,V4,V5) i want calculate the average of this five vectors.

请先登录,再进行评论。


Image Analyst
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.

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by