Matlab 在向量B的值处将向量A转换为矩阵M
我有两个向量,两个向量的格式都是Matlab 在向量B的值处将向量A转换为矩阵M,matlab,matrix,vector,Matlab,Matrix,Vector,我有两个向量,两个向量的格式都是double。 向量“A”是mx2。第一列包含连续的时间步长,第二列包含测量序列中的数据 向量“B”是一个nx2(1以下代码应执行此操作: 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
double
。
向量“A”是mx2。第一列包含连续的时间步长,第二列包含测量序列中的数据
向量“B”是一个nx2
(1以下代码应执行此操作:
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(:,:).');
这是问题的代码。我计算了vect B的两个时间戳之间的差异。然后我检查了A中的相同时间戳,并替换了A(2)中M中的val
变量数,并对所有A进行迭代。添加从额外嵌套for循环中保存的val变量,在该循环中我必须找到B(I,2)
@PranavTotala建议进行编辑,他注意到在示例中,B
比A
小,但在文本中,它们的大小相同。请修复您的帖子。哦,抱歉这个错误。我只想说,A
和B
都有两列,但行数可能不同。但是当然,它们的大小不一样。A
和B
是单元格数组吗?时间戳是编码为数值还是字符串?A
和B
格式为double
。因此,B
中的时间戳以及A
中的时间序列都编码为数值。谢谢这段代码可能会提供一些有限的、即时的帮助。A通过说明为什么这是一个很好的问题解决方案,并使它对未来读者提出其他类似问题更有用。请您的答案添加一些解释,包括您所做的假设。这不是Matlab,无论如何,请检查更新的回答。希望它符合标准。如果它足够,请删除下一票,因为这会让人泄气。另外,我在octave online上编写了prev代码,这可能是语法不正确的原因。嗨@TommasoBelluzzo,谢谢你的代码!我已经用我的数据尝试过了,在创建矩阵之前一切都正常。我想我应该我提到,A
和B
都被格式化为double
。我从一个非单元格数组对象获取错误单元格内容引用。vector2matrix中的错误>@(x)[[A{x,2}],零(1,fill numel(x))]vector2matrix中的错误(第88行)mat=cellfun(@(x)[[A{x,2}]零(1,fill numel(x))],ran,'UniformOutput',false)
是的,嗯……这可能是因为不匹配……试着用(x,2)替换{x,2},希望这能解决问题,因为我目前无法测试我的脚本。将A{x,2}
更改为A(x,2)
还创建了一个错误:使用被连接矩阵的horzcat维度的错误不一致。向量2矩阵中的错误>@(x)[[A(x,2)],零(1,fill numel(x))]向量2矩阵中的错误(第88行)mat=cellfun(@(x)[[A(x,2)]零(1,fill numel(x))],ran,'UniformOutput',false);
你的矩阵可能有不同的方向。在匿名函数上安装一个调试器并检查它。答案已编辑,它现在应该可以处理数字矩阵了。
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(:,:).');
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