how to calculate a average of five arrays?

조회 수: 16 (최근 30일)
Biza Ferreira
Biza Ferreira 2016년 8월 19일
답변: Image Analyst 2016년 8월 20일
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?
Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 19일
Your code is not clear

댓글을 달려면 로그인하십시오.

답변 (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