Mean of parts of an array

27 vues (au cours des 30 derniers jours)
Andrew
Andrew le 19 Août 2014
Modifié(e) : Adam le 20 Août 2014
Good day to all,
I have searched for an answer can't seem to find it so here goes my description.
I have an array called MeanOfSegments which is 152x1 double (see figure)
I wish to take the mean of parts of this array. This worked great for my first few variables which were consecutive. Here is the successful code I used
if true
% code
MeanVariable1 = mean(MeanOfSegments(1:7));
end
Now I wish to create a variable called MeanVariable2 which is the mean of cell 8,12,13,17. I have tried the following code with no success.
if true
% code
MeanVariable2 = mean(MeanOfSegments(8,12,13,17));
end
Does anyone know how this can be obtained? I am using R2014a
Thanks for any and all help. Andrew

Réponse acceptée

Adam
Adam le 19 Août 2014
Try putting your 8, 12, 13, 17 in square brackets as
MeanVariable2 = mean(MeanOfSegments( [8,12,13,17] ));

Plus de réponses (1)

Andrew
Andrew le 19 Août 2014
Well that was ridiculously easy. Thank you very much for your answer. I'm new to MatLab syntax. Thank you very very much.
  1 commentaire
Adam
Adam le 20 Août 2014
Modifié(e) : Adam le 20 Août 2014
That syntax just means that all your indices are in an array so they can then be used for non-contiguous indexing.
Passing 4 comma delimited numbers in as they are would be interpreted by Matlab as though you are trying to index into a 4-dimensional matrix.
I get used to many of these things just by trying out syntaxes on the command line so you can pick things up quite quickly like that!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by