Cellfun for mean squared error

1 vue (au cours des 30 derniers jours)
Phoebe Daphne
Phoebe Daphne le 23 Mai 2021
Commenté : Phoebe Daphne le 23 Mai 2021
I have a 450x2 cell data structure called data, with each cell containing 10000 x 1 Doubles.
I want to calculate the mean squared error (via the immse function) between for each row in the cell, i.e. immse(data{i,1},data{i,2}) with i indicating one of the 450 rows. Then I want to add the calculated mean squared error to a variable called data_sum (My overall goal is to calculate the average mean squared error).
So far, I am solving this by looping over data:
data_sum = 0;
for p = 1:size(data,1)
data_sum = data_sum + immse(data{p,1},data{p,2});
end
data_avg = data_sum / size(data,1);
I want to optimize my code and use cellfun instead. So far I have tried the following:
C = cellfun(@immse, data{:,1}, data{:,2}, 'UniformOutput', false);
However, this gives me the error:
Error using cellfun
Input #2 expected to be a cell array, was double instead.
I read online that I should convert double to cell to solve this problem. Since I am using cellfun because, well, I am applying it to a cell, it makes me assume that I am indexing data in a wrong way, because otherwise I wouldn't have to convert it to a cell first. Or should I be using a different function than cellfun?
  2 commentaires
Matt J
Matt J le 23 Mai 2021
cellfun will not make the code run faster. It will just reduce the number of lines of code.
Phoebe Daphne
Phoebe Daphne le 23 Mai 2021
I want to reduce my lines of code.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 23 Mai 2021
Modifié(e) : Matt J le 23 Mai 2021
Seems like you could do the whole thing in one line with,
data_avg = immse( cell2mat(data(:,1)) , cell2mat(data(:,2)) );
  1 commentaire
Phoebe Daphne
Phoebe Daphne le 23 Mai 2021
Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion 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