如何根据两列对矩阵进行排序

发布于 2024-11-19 21:23:09 字数 320 浏览 1 评论 0原文

我正在尝试使用 MATLAB 根据两列对单元格矩阵进行排序:

ticker date price
msft 1/9/11 19.8
msft 1/8/11 18.7
csco 1/8/11 9.8
csco 1/9/11 10.0

我想先按日期排序矩阵,然后按代码排序,所以我想要的结果是:

ticker date price
csco 1/8/11 9.8
msft 1/8/11 18.7
csco 1/9/11 10.0
msft 1/9/11 19.8

有人知道我该怎么做吗?谢谢。

I am trying to sort a cell matrix based on two columns using MATLAB:

ticker date price
msft 1/9/11 19.8
msft 1/8/11 18.7
csco 1/8/11 9.8
csco 1/9/11 10.0

I want to sort the matrix first by date then by ticker, so the result I want is:

ticker date price
csco 1/8/11 9.8
msft 1/8/11 18.7
csco 1/9/11 10.0
msft 1/9/11 19.8

Anyone knows how I can do that? THanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枉心 2024-11-26 21:23:09

我会将日期列转换为相应的日期序列号 datenum并使用 sortrows 按照所需的顺序进行排序 数据。

这是一些代码;假设 d 是您给出的示例中的 4x3 元胞数组:

<前><代码>d =

“微软”“2011 年 1 月 9 日”[19.8000]
“微软”“2011 年 1 月 8 日”[18.7000]
“csco”“2011 年 1 月 8 日”[ 9.8000]
“csco”“2011 年 1 月 9 日”[ 10]

I would convert the date column into the corresponding serial date number with datenum and use sortrows with the required ordering to sort the data.

Here's some code ; assume d is your 4x3 cell array from the example you gave:

d = 

'msft'    '1/9/11'    [19.8000]
'msft'    '1/8/11'    [18.7000]
'csco'    '1/8/11'    [ 9.8000]
'csco'    '1/9/11'    [     10]
  • Convert the date column to numbers with arrayfun

    d(:,2) = arrayfun(@(x){datenum(x)},d(:,2));
    
  • Sort the matrix with sortrows (date first then ticker symbol)

    d = sortrows(d,[2 1]);
    
  • Replace the date with a string with datestr.

    d(:,2) = arrayfun(@(x){datestr(x{1},'mm/dd/yy')},d(:,2));
    
高跟鞋的旋律 2024-11-26 21:23:09

将两个元胞数组连接成一个字符串元胞数组。对生成的字符串元胞数组进行排序并获取索引的顺序。 按照 Jacob 的建议,使用这些索引对原始元胞数组进行排序

a = {'1', '2', '2', '3'}
b = {'a', 'b', 'a', 'a'}
for i = 1:length(a)
   ab{i} = [a{i},b{i}]
end
[s,si] = sort(ab);
sorted_a = a(si);
sorted_b = b(si);

,如果您使用的日期格式不能按字母顺序排序,您可以替换

   ab{i} = [a{i},b{i}]

   ab{i} = [num2str(datenum(a{i})),b{i}]

concatenate the two cell arrays into a single cell array of strings. Sort the resultant cell array of strings and get the order of the indicies. Use those indicies to sort the original cell arrays

a = {'1', '2', '2', '3'}
b = {'a', 'b', 'a', 'a'}
for i = 1:length(a)
   ab{i} = [a{i},b{i}]
end
[s,si] = sort(ab);
sorted_a = a(si);
sorted_b = b(si);

following Jacob's suggestion, if the date format you are using is not sortable alphabetically you can replace

   ab{i} = [a{i},b{i}]

with

   ab{i} = [num2str(datenum(a{i})),b{i}]
把回忆走一遍 2024-11-26 21:23:09

使用类似于 @Jacob< /a> 的,这是一个稍微简单的解决方案:

%# your cell array
d = {
    'msft' '1/9/11' 19.8
    'msft' '1/8/11' 18.7
    'csco' '1/8/11'  9.8
    'csco' '1/9/11' 10.0
};

%# extract 2 columns, convert to serial date, sort, apply order to original data
[~,idx] = sortrows([datenum(d(:,2),'mm/dd/yy') cell2mat(d(:,3))], [1 2]);
d = d(idx,:)

之前:

d = 
    'msft'    '1/9/11'    [19.8]
    'msft'    '1/8/11'    [18.7]
    'csco'    '1/8/11'    [ 9.8]
    'csco'    '1/9/11'    [  10]

之后:

d = 
    'csco'    '1/8/11'    [ 9.8]
    'msft'    '1/8/11'    [18.7]
    'csco'    '1/9/11'    [  10]
    'msft'    '1/9/11'    [19.8]

Using a similar idea to @Jacob's, here is a slightly easier solution:

%# your cell array
d = {
    'msft' '1/9/11' 19.8
    'msft' '1/8/11' 18.7
    'csco' '1/8/11'  9.8
    'csco' '1/9/11' 10.0
};

%# extract 2 columns, convert to serial date, sort, apply order to original data
[~,idx] = sortrows([datenum(d(:,2),'mm/dd/yy') cell2mat(d(:,3))], [1 2]);
d = d(idx,:)

The before:

d = 
    'msft'    '1/9/11'    [19.8]
    'msft'    '1/8/11'    [18.7]
    'csco'    '1/8/11'    [ 9.8]
    'csco'    '1/9/11'    [  10]

The after:

d = 
    'csco'    '1/8/11'    [ 9.8]
    'msft'    '1/8/11'    [18.7]
    'csco'    '1/9/11'    [  10]
    'msft'    '1/9/11'    [19.8]
梦初启 2024-11-26 21:23:09
[tblB,index] = sortrows(tblA,{'Height','Weight'},{'ascend','descend'})

针对您的具体情况:

ticker date price
msft 1/9/11 19.8
msft 1/8/11 18.7
csco 1/8/11 9.8
csco 1/9/11 10.0

 

[tblB,index] = sortrows(tblA,{'date','price'},{'ascend','descend'})
[tblB,index] = sortrows(tblA,{'Height','Weight'},{'ascend','descend'})

for your specific case:

ticker date price
msft 1/9/11 19.8
msft 1/8/11 18.7
csco 1/8/11 9.8
csco 1/9/11 10.0

 

[tblB,index] = sortrows(tblA,{'date','price'},{'ascend','descend'})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文