Effacer les filtres
Effacer les filtres

how to find mean from cell

1 vue (au cours des 30 derniers jours)
Thar
Thar le 20 Jan 2015
Modifié(e) : Star Strider le 20 Jan 2015
Hi all!
I have the matrix:
M=[Finally(:,1:8)];
S=[Finally(:,9:2012)];
and
Mc= mat2cell(M, rows, 8);
Sc= mat2cell(S, rows, 2004);
I want to find the means of Mc and Sc.
I have the script
for k1 = 1:size(Mc,1)
W(k1)=mean(Mc{k1}(1:end,1));
end
but it doesn't work.
Is anything wrong?
Thank you!
  2 commentaires
Guillaume
Guillaume le 20 Jan 2015
You'll have to clarify, the 'it doesn't work' by posting the entire error message that you get (including the line that errors out).
As it is, your code should work. Here is a slight rewrite:
M = rand(100, 8); %demo data
rows = 20 * ones(1, size(M, 1)/20); %demo data
Mc = mat2cell(M, rows, size(M, 2));
W = cellfun(@(m) mean(m(:, 1)), Mc);
Star Strider
Star Strider le 20 Jan 2015
Modifié(e) : Star Strider le 20 Jan 2015
This looks familiar.
Thodoris, how do you calculate your ‘rows’ variable? That is likely what we will need to Answer your Question.

Connectez-vous pour commenter.

Réponses (1)

Titus Edelhofer
Titus Edelhofer le 20 Jan 2015
Hi,
first of all, why do you convert the matrix to a cell? Why not call mean for M? It should give you the mean for all columns directly. Second, using Mc{k1}(1:end,1) can be simplified by Mc{k1}(:,1). Third, "It doesn't work" sounds like you get an error message. Tell us the error message and it will be easier for us to diagnose.
Titus
  1 commentaire
Thar
Thar le 20 Jan 2015
I want to convert the matrix to a cell, because i have the step for the mean values to the rows. The message is that the dimensions are wrong.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by