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!