Looping Over Cell Array with if statements

2 次查看(过去 30 天)
I'm trying to loop over a cell array and create a new cell array which in the 6th column contains only NaNs and values > than the value of the 99th percentile (so I can calculate the nanmean of the 6th column etc). At the moment this script runs but the 6th column of the new cell array (values_above_99th_percentile) contains only NaNs and each loop iteration isnt adding the values > than the 99th percentile.
Any ideas?
%define an array of 68 Nans to add means to
mean_of_values_above_99th_percentile = nan(1,N);
% define an array of 68 values where each value corresponds to an annual
% percentile
percentile = nan(1,N);
for k = 1:N
percentile(k) = (prctile(cell{k}(:,6),99));
end
% create a cell array in which each (:,6) corresponds to nan, so that
% values > than 99th percentile can be added to it
values_above_99th_percentile = cell(1,:);
for k = 1:N
values_above_99th_percentile{k}(:,6) = nan;
end
% add values > than 99th percentile to cell array
% PROBLEM IN THIS LOOP
for k = 1:N
if cell{k}(:,6) > percentile(k);
values_above_99th_percentile{k}(:,6) = (cell{k}(:,6));
elseif cell{k}(:,6) < percentile(k);
values_above_99th_percentile{k}(:,6) = nan;
end
end
% calculate mean of values above 99th percentile
for k = 1:N
mean_of_values_above_99th_percentile(k) = nanmean(values_above_99th_percentile{k}(:,6));
end
  2 个评论
Adam
Adam 2017-2-7
Don't name an array 'cell', this is the builtin data type and over-riding it will have all sorts of unwanted effects.
Probably that isn't the cause of your specific problem, but it is what jumps out at me first.
Stephen23
Stephen23 2017-2-7
编辑:Stephen23 2017-2-7
Related earlier question:
And never call a variable cell, length, size, mean, i, j, string, etc, etc. Use which to see if a name is already used.

请先登录,再进行评论。

回答(2 个)

Guillaume
Guillaume 2017-2-7
编辑:Guillaume 2017-2-7
The first issue is that you've called your variable cell which will prevent you from using the function cell to create cell arrays
Then you say "create a new cell array which in the 6th column", but your indexing is cell{k}(:, 6). This does not index the 6th column of the cell array, but the 6th column of whatever (a matrix?) is in cell k.
Then there is
values_above_99th_percentile = cell(1,:);
If the badly named cell is a row vector, this simply copies the whole cell array into the new variable, but would be written more clearly as:
values_above_99th_percentile = cell; %please change variable name
If it's a column vector, then it only copies the first cell, in which case:
values_above_99th_percentile = cell(1); %please change variable name
If the cell array is itself 2D (there's no evidence that it is since you always use linear indexing to iterate over the cells, then it copies the first row only
Then the if in the loop does not work. This may:
%replaces the above assignment, the loops that puts NaN in it, and the if loop:
values_above_99th_percentile = cell; %please change variable name
for k = 1:N
values_above_99th_percentile{k}(cell{k}(:, 6) < percentile(k), 6) = nan;
end

Adam
Adam 2017-2-7
if cell{k}(:,6) > percentile(k)
is not going to do what you want unless cell{k}(:,6) evaluates to a scalar which I assume it doesn't since you use ':'
If you want to test greater than type expressions of a vector then you need to use a qualifier like 'all' or 'any', otherwise you get a vector of booleans from
cell{k}(:,6) > percentile(k)
and the if statement uses an implicit 'all' on these

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by