How can I skip empty cells when averaging during a function?
10 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Chunru
on 3 Jun 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 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!