Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 基于带有NAN的列对单元格数组进行排序_Matlab_Sorting - Fatal编程技术网

Matlab 基于带有NAN的列对单元格数组进行排序

Matlab 基于带有NAN的列对单元格数组进行排序,matlab,sorting,Matlab,Sorting,我的数据是一个名为PM25的1x7单元。在每个单元内,还有另一个单元的大小为365x5xN,其中N不同。下面是PM25{1,1}的一部分(可以在这里找到数据:。所讨论的变量是PM25) 我试图通过最后一列,浓度,对整个细胞进行分类。以下是我一直在做的事情: % Sort each site based on the concentration values - Descending order with NaN's at the bottom for i = 1:length(names_PM2

我的数据是一个名为PM25的1x7单元。在每个单元内,还有另一个单元的大小为365x5xN,其中N不同。下面是PM25{1,1}的一部分(可以在这里找到数据:。所讨论的变量是PM25)

我试图通过最后一列,浓度,对整个细胞进行分类。以下是我一直在做的事情:

% Sort each site based on the concentration values - Descending order with NaN's at the bottom
for i = 1:length(names_PM25_O3) % States
    for j = 1:length(PM25{i}(1,1,:)) % Number of sites
        [~,ix] = sort(str2double(PM25{i}(:,5,j))); % Sorted indices
        nanmask = isnan(str2double(PM25{i}(ix,5,j))); % Get mask (0 or 1) of nan-rows to be ignored
        ix = flipdim(ix(~nanmask),1); % Get non-nan indices in reverse order
        PM25_sorted{i} = PM25{i}(ix,:,:); % Sort
    end
end
问题是,这个代码只对PM25中7个单元中的最后N个单元进行排序。所有其他N都是根据最后N进行排序的,我得到的值少于365,可能是因为在最后N中删除了NaN

例如,这里是N=1(PM25{1,1}(:,:,1))的一部分

虽然这是PM25{1,1}(:,:,21)中最后N,N=21的一部分

如您所见,N=21按降序排序,所有的NaN都消失了。但是N=1只是按N=21的顺序排列(看第4列,日期-顺序相同),所以它不是按降序排列的

我怎样才能让整个细胞单独分类呢?我可能必须保留NaN行,否则,每个N都将是不同的长度。目前,它们似乎正在从排序的N.函数中删除。

-

function sorted_cell_array = sortcell_col5(org_cell_array)

col5 = org_cell_array(:,5);
isnum = cellfun(@isnumeric,col5);
t2 = NaN(size(org_cell_array,1),1);
t2(~isnum) = str2num(char(col5(~isnum)));
[~,y1] = sort(t2);
c1 = nnz(~isnan(t2));
if ~c1
    sorted_cell_array = org_cell_array(y1,:);
else
    ind1 = [ flipud(y1(1:c1)) ; y1(c1+1:end) ];
    sorted_cell_array = org_cell_array(ind1,:);
end

return;
主脚本-

load data_2007.mat %%// Load your data mat file

PM25_sorted = PM25;
M1 = size(PM25,2);
for k1 = 1:M1
    [sz1,sz2,N] = size(PM25{1,k1});
    for k2 = 1:N
        PM25_sorted{1,k1}(:,:,k2) = sortcell_col5(PM25{1,k1}(:,:,k2));
    end
end

某种程度上。输出是我想要的,但是我拥有的单元格数组不仅仅是我粘贴的示例。它有几层深。你可以在我发布的链接中看到。@shizishan你想要数字后面的
NaNs
,对吗?我会尝试一下(目前不行,因为互联网速度很慢,我正在远程运行),并进行修改。“我的单元格是3D的,所以有点不同。”狮子山编辑时几乎没有改动,同时也要记住,当前代码将按升序排序。对于降序排序,只需在
sort
命令中添加术语“descent”。一个问题是:当我按降序排序时,所有的NaN都在顶部。如果我想找出前36个值,我不知道这些值从哪里开始。是否存在排序下降,但将NaN放在底部的方法?
'42.695391'    '-93.655976'    '19-197-0004'    [733396]    '48'  
'42.695391'    '-93.655976'    '19-197-0004'    [733393]    '36.4'
'42.695391'    '-93.655976'    '19-197-0004'    [733108]    '33.3'
'42.695391'    '-93.655976'    '19-197-0004'    [733207]    '25.4'
'42.695391'    '-93.655976'    '19-197-0004'    [733366]    '24.3'
'42.695391'    '-93.655976'    '19-197-0004'    [733255]    '22.4'
'42.695391'    '-93.655976'    '19-197-0004'    [733063]    '21'  
'42.695391'    '-93.655976'    '19-197-0004'    [733225]    '20'  
'42.695391'    '-93.655976'    '19-197-0004'    [733066]    '19.8'
'42.695391'    '-93.655976'    '19-197-0004'    [733250]    '19.6'
'42.695391'    '-93.655976'    '19-197-0004'    [733387]    '19.5'
'42.695391'    '-93.655976'    '19-197-0004'    [733153]    '19.2'
'42.695391'    '-93.655976'    '19-197-0004'    [733384]    '18.8'
function sorted_cell_array = sortcell_col5(org_cell_array)

col5 = org_cell_array(:,5);
isnum = cellfun(@isnumeric,col5);
t2 = NaN(size(org_cell_array,1),1);
t2(~isnum) = str2num(char(col5(~isnum)));
[~,y1] = sort(t2);
c1 = nnz(~isnan(t2));
if ~c1
    sorted_cell_array = org_cell_array(y1,:);
else
    ind1 = [ flipud(y1(1:c1)) ; y1(c1+1:end) ];
    sorted_cell_array = org_cell_array(ind1,:);
end

return;
load data_2007.mat %%// Load your data mat file

PM25_sorted = PM25;
M1 = size(PM25,2);
for k1 = 1:M1
    [sz1,sz2,N] = size(PM25{1,k1});
    for k2 = 1:N
        PM25_sorted{1,k1}(:,:,k2) = sortcell_col5(PM25{1,k1}(:,:,k2));
    end
end