how to calculate a average of five arrays?

22 visualizzazioni (ultimi 30 giorni)
Biza Ferreira
Biza Ferreira il 19 Ago 2016
Risposto: Image Analyst il 20 Ago 2016
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 Commenti
James Tursa
James Tursa il 19 Ago 2016
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 il 19 Ago 2016
Your code is not clear

Accedi per commentare.

Risposte (2)

Azzi Abdelmalek
Azzi Abdelmalek il 19 Ago 2016
If you want to calculate the mean of any matrix, you can use mean function
mean(A)
  1 Commento
Biza Ferreira
Biza Ferreira il 19 Ago 2016
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.

Accedi per commentare.


Image Analyst
Image Analyst il 20 Ago 2016
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.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by