我有两个向量,格式为double
。
矢量“A”是mx2。第一列包含连续的时间步长和来自测量系列的第二列数据。
向量“B”是一个nx2 (1<n<m)
,包含几个与向量“A”中的某些时间步长匹配的时间戳。
现在我想将矢量“A”拆分成矩阵“M”。 Matrix应该开始一个新列,每次“A”的时间步长与向量“B”中第一列中的时间戳相对应,并在它们与第二列中的时间对应时结束。
因此,在“B”的每一行中,两个时间戳定义矩阵“M”的列。
由于两个时间戳之间的序列长度不同,因此每行应填充0到最多一行。例如第5行。
矩阵应该只包含测量数据而不包含任何时间戳。矩阵的格式应为double
。
在一个例子中,它看起来像:
vector A [18 x 2]:
1. column [11:13:30 11:13:31 11:13:32 11:13:33 11:13:34 11:13:35 11:13:36 11:13:37 11:13:38 11:13:39 11:13:40 11:13:41 11:13:42 11:13:43 11:13:44 11:13:45 11:13:46 11:13:47]
2. column [6000, 6500, 5000, 8000, 15000, 15500, 16000, 6000, 4000, 16500, 14000, 400, 5000, 6000, 9000, 12000, 13000, 5000]
vector B [3 x 2]:
1. column [11:13:33 11:13:39 11:13:44]
2. column [11:13:36 11:13:40 11:13:46]
matrix M [3 x 6]:
1. column [8000 15000 15500 16000 0]
2. column [16500 14000 0 0 0]
3. column [9000 12000 13000 0 0]
这对我来说似乎是一件非常复杂的事情,我希望任何人都可以帮助我。
答案 0 :(得分:2)
以下代码应该完成这项工作:
A = {
'11:13:30' 6000;
'11:13:31' 6500;
'11:13:32' 5000;
'11:13:33' 8000;
'11:13:34' 15000;
'11:13:35' 15500;
'11:13:36' 16000;
'11:13:37' 6000;
'11:13:38' 4000;
'11:13:39' 16500;
'11:13:40' 14000;
'11:13:41' 400;
'11:13:42' 5000;
'11:13:43' 6000;
'11:13:44' 9000;
'11:13:45' 12000;
'11:13:46' 13000;
'11:13:47' 5000
};
B = {
'11:13:33' '11:13:36';
'11:13:39' '11:13:40';
'11:13:44' '11:13:46';
};
fill = 5;
% Obtain the computation parameters from A...
A_len = size(A,1);
A_seq = (1:A_len).';
% Find the breakpoints of A using the values in B...
idx_beg = ismember(A(:,1),B(:,1)) .* A_seq;
idx_beg = idx_beg(idx_beg > 0);
idx_end = ismember(A(:,1),B(:,2)) .* A_seq;
idx_end = idx_end(idx_end > 0);
%Compute the maximum number of elements per row...
rows = max(idx_end - idx_beg + 1);
% Adjust the fill in order to cover enough elements...
fill = max([fill rows]);
% Create the row indexers of A based on the breakpoints...
ran = arrayfun(@(x)idx_beg(x):idx_end(x),1:numel(idx_beg),'UniformOutput',false);
% Create the final matrix with zero-padding on the right...
mat = cellfun(@(x)[[A{x,2}] zeros(1,fill-numel(x))],ran,'UniformOutput',false);
mat = cell2mat(mat(:,:).');
最终输出:
mat =
8000 15000 15500 16000 0
16500 14000 0 0 0
9000 12000 13000 0 0
代码非常明确,但如果您需要澄清,请随时在下面的评论中提出。
修改
A = [
30 6000;
31 6500;
32 5000;
33 8000;
34 15000;
35 15500;
36 16000;
37 6000;
38 4000;
39 16500;
40 14000;
41 400;
42 5000;
43 6000;
44 9000;
45 12000;
46 13000;
47 5000
];
B = [
33 36;
39 40;
44 46;
];
fill = 5;
% Obtain the computation parameters from A...
A_len = size(A,1);
A_seq = (1:A_len).';
% Find the breakpoints of A using the values in B...
idx_beg = ismember(A(:,1),B(:,1)) .* A_seq;
idx_beg = idx_beg(idx_beg > 0);
idx_end = ismember(A(:,1),B(:,2)) .* A_seq;
idx_end = idx_end(idx_end > 0);
%Compute the maximum number of elements per row...
rows = max(idx_end - idx_beg + 1);
% Adjust the fill in order to cover enough elements...
fill = max([fill rows]);
% Create the row indexers of A based on the breakpoints...
ran = arrayfun(@(x)idx_beg(x):idx_end(x),1:numel(idx_beg),'UniformOutput',false);
% Create the final matrix with zero-padding on the right...
mat = cellfun(@(x)[A(x,2).' zeros(1,fill-numel(x))],ran,'UniformOutput',false);
mat = cell2mat(mat(:,:).');
答案 1 :(得分:0)
以下是问题的代码。我计算了vect B的两个时间戳之间的差异。然后我在A中检查了相同的时间戳,并从A(2)中替换了M中的val
个变量,并将其迭代为所有A.添加保存的val变量一个额外的嵌套for循环,我必须找到B(i,2)。
a=length(A(:,1));
b=length(B(:,1));
M= zeros(5,b);
i=1;
for j=1:a
if B{i,1} == A{j,1}
durn_vect=B{i,2}-B{i,1};
val=durn_vect(4)*600+durn_vect(5)*60+durn_vect(7)*10+durn_vect(8)+1;
for k=1:val
M(k,i)=A{j+k-1,2};
end
i=i+1;
end
if(i>3)
break
end
end