How can I skip empty cells when averaging during a function?
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Chunru
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!