1

在 MATLAB 中,我试图按日历年“ m ”(nxtPdOut(nn).datePdLow )过滤掉结构nxtPdOut,并将每个nxtPdOut(nn)元素插入到新结构yoyComp(m)中。希望这不会太令人困惑,但我会尝试进一步解释。

以下代码将构建结构nxtPdOut(注意:此结构不是静态的,并且会随着未知的额外年份而变化):

nxtPdOut(1).datePdLow = 734505;
nxtPdOut(1).a = 111;
nxtPdOut(1).b = 222;
nxtPdOut(2).datePdLow = 734510;
nxtPdOut(2).a = 333;
nxtPdOut(2).b = 444;
nxtPdOut(3).datePdLow = 734868;
nxtPdOut(3).a = 888;
nxtPdOut(3).b = 887;

nxtPdOut(4).datePdLow = 734869;
nxtPdOut(4).a = 555;
nxtPdOut(4).b = 666;
nxtPdOut(5).datePdLow = 734872;
nxtPdOut(5).a = 777;
nxtPdOut(5).b = 888;
nxtPdOut(6).datePdLow = 734880;
nxtPdOut(6).a = 999;
nxtPdOut(6).b = 1010;

nxtPdOut(7).datePdLow = 735235;
nxtPdOut(7).a = 999;
nxtPdOut(7).b = 1010;
nxtPdOut(8).datePdLow = 735300;
nxtPdOut(8).a = 999;
nxtPdOut(8).b = 1010;

以下代码将确定要排序的m年(注意:m年不会是恒定的,并且总是会根据 numYrs 变化):

numYrs = 2;
tDay = datevec(date);
for m=1:1:numYrs+1
    if m == 1
        yrs(:,:,m) = [tDay(1,1) tDay(1,2)-tDay(1,2)+1 tDay(1,3)-...
            tDay(1,3)+1 0 0 0];
    else
        yrs(:,:,m) =  [yrs(1,1,1)-m+1 1 1 0 0 0];
    end
end
jj = size(yrs(1,1,:));
for j=1:1:jj(3)
    yrsStr(1,j) = datenum(yrs(:,:,j));
end
yrsStr = fliplr(yrsStr); %ascending serial numbers

% Output for yrsStr: 
% yrsStr = [734504      734869      735235]
% FYI:  (datestr(yrsStr(1)) == 01-Jan-2011; 
%    datestr(yrsStr(2)) == 01-Jan-2012; 
%    datestr(yrsStr(1)) == 01-Jan-2013

我尝试了以下代码的不同组合,但被卡住了,不知道如何完成它。

nn = 1;
for n = 1:length(yrsStr)-1
    yoyComp(n).signals = nxtPdOut(nn).(nxtPdOut.datePdLow >= yrsStr(n) &...
        nxtPdOut(nn).datePdLow < yrsStr(n+1));
end

    while nn <= numel(nxtPdOut)
        if nxtPdOut(nn)
            yoyComp(n).signals = nxtPdOut

我正在尝试按日历年过滤nxtPdOut并将结果放入新结构yoyComp中。例如,734504 <= elements < 734869 进入 yoyComp(1);734869 <= 元素 < 735235 进入 yoyComp(2); 最后,元素 >= 735235 进入 yoyComp(3)。有关所需的输出,请参阅以下内容:

yoyComp(1).out(1).datePdLow = 734505;
yoyComp(1).out(1).a = 111;
yoyComp(1).out(1).b = 222;
yoyComp(1).out(2).datePdLow = 734510;
yoyComp(1).out(2).a = 333;
yoyComp(1).out(2).b = 444;
yoyComp(1).out(3).datePdLow = 734868;
yoyComp(1).out(3).a = 888;
yoyComp(1).out(3).b = 887;

yoyComp(2).out(1).datePdLow = 734869;
yoyComp(2).out(1).a = 555;
yoyComp(2).out(1).b = 666;
yoyComp(2).out(2).datePdLow = 734872;
yoyComp(2).out(2).a = 777;
yoyComp(2).out(2).b = 888;
yoyComp(2).out(3).datePdLow = 734880;
yoyComp(2).out(3).a = 999;
yoyComp(2).out(3).b = 1010;

yoyComp(3).out(1).datePdLow = 735235;
yoyComp(3).out(1).a = 999;
yoyComp(3).out(1).b = 1010;
yoyComp(3).out(2).datePdLow = 735300;
yoyComp(3).out(2).a = 999;
yoyComp(3).out(2).b = 1010;

如果有任何想法/建议继续,我将不胜感激。我知道如何从矩阵中过滤/提取,但我不知道如何通过结构来做到这一点。请指教。谢谢你。

4

1 回答 1

1

You should add a final inf term to your yrsStr and then you can use the amended code at the bottom of this answer to create your yoyComp structure. The main change I've made has been to use logical indexing, and capture the output of the structure's fields by surrounding the expression with square brackets (see [nxtPdOut.datePdLow]).

Here's a basic demonstration of what I mean

For the structure

a(1).b = 5
a(2).b = 6
a(3).b = 7

You can interact with it in the following way, either:

>> a.b

ans =

     5


ans =

     6


ans =

     7

Or by capturing the results into a vector:

>> [a.b]

ans =

     5     6     7

For your code specifically, the following gives the result that you described in the question:

yrsStr = [yrsStr, inf];

nn = 1;
for n = 1:length(yrsStr)-1
    yoyComp(n).signals = nxtPdOut([nxtPdOut.datePdLow] >= yrsStr(n) &...
        [nxtPdOut.datePdLow] < yrsStr(n+1));
end

Note that I've deleted that I've deleted the

while nn <= numel(nxtPdOut)
    if nxtPdOut(nn)
        yoyComp(n).signals = nxtPdOut

section of your code since I'm not sure what it was doing.

于 2013-06-26T07:09:49.810 回答