Looping Over Cell Array with if statements

2 Ansichten (letzte 30 Tage)
Lewis Holden
Lewis Holden am 7 Feb. 2017
Bearbeitet: Guillaume am 7 Feb. 2017
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 Kommentare
Adam
Adam am 7 Feb. 2017
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 am 7 Feb. 2017
Bearbeitet: Stephen23 am 7 Feb. 2017
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Guillaume
Guillaume am 7 Feb. 2017
Bearbeitet: Guillaume am 7 Feb. 2017
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 am 7 Feb. 2017
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

Kategorien

Mehr zu Creating and Concatenating Matrices 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!

Translated by