How can I skip empty cells when averaging during a function?
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I have a function in which I average the values of the doubles within each cell of a cell array.
for i = 1:length(explained_balls)
explained_pc1_avg_part_x_phase(1,2) = mean(explained_balls{i}(1,:));
end
Because I have a cell which is empty I get the error
Index in position 1 exceeds array bounds.
I was wondering if I can write the functiion so that it skips the empty cell and doesnt throw the error? Help is greatly appreciated.
0 commentaires
Réponses (1)
Chunru
le 3 Juin 2022
load explained_balls
whos
for i = 1:length(explained_balls)
%explained_balls{i}
if isempty(explained_balls{i})
explained_pc1_avg_part_x_phase(i) = NaN; % or 0
else
% explained_pc1_avg_part_x_phase(1,2) = mean(explained_balls{i}(1,:));
% The above find average of the 1st row, which is a single number.
% Check it out!!!
explained_pc1_avg_part_x_phase(i) = mean(explained_balls{i});
end
end
explained_balls
explained_pc1_avg_part_x_phase
0 commentaires
Voir également
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!