我有一个double matrix B,在C1中有1998年,在C2中有一个代码(不重复)。每个C2都在C3中得到一个值。
% C1 C2 C3
B=[ 1998 22 37
1998 34 7
1998 76 12
1998 98 29
1998 107 14
…]我就是这样得到B的
N1=[N{:,1} N{:,2} N{:,3}];
N1=unique(N1(:,1:3),'rows');
N3= unique(N1(:,1:2),'rows');
for m=1:size(N3,1)
N3(m,3)=sum(N1(:,1) == N3(m,1) & N1(:,2)==N3(m,2));
end
B=N3((N3(:,1) == 1998),:);我有一个cell-array A,其年水平配置为R1,未重复值在Y中,相应的代码出现在后面的列中。代码与变量C2 B**.**中的中的代码匹配。
A={Y 1996 1997 1998 1999 %R1
1 107 107 22 22
13 98 98 76 1267
… }有什么方法可以让我获得一个新的变量来识别变量A中代码的变化,并在B中显示来自C3的相应值?例如:
AB={Y Initial C2 Change C2
1 107 14 22 37
13 98 29 76 12 }发布于 2014-07-20 20:50:08
AB = {'Y' 'Initial' 'C2' 'Change' 'C2'}; % initialize result cell array
for i=2:size(A,1) % loop through rows of A
indicesOfChanges = find(diff([A{i,2:end}])); % find changes in row
for j=1:length(indicesOfChanges) % for all changes look up the corresponding values
ind1=find(B(:,2)==A{i,indicesOfChanges(j)+1}); % row index in B of value before change
ind2=find(B(:,2)==A{i,indicesOfChanges(j)+2}); % row index in B of value after change
AB{end+1,1} = A{i,1};
AB{end,2} = B(ind1,2);
AB{end,3} = B(ind1,3);
AB{end,4} = B(ind2,2);
AB{end,5} = B(ind2,3);
end
end也许使用矢量化方法可以进一步改进这一点,但只要数组不太大,使用循环的速度就应该足够快。
https://stackoverflow.com/questions/24853496
复制相似问题