添加长度不同的数组
Add arrays that do not have the same length
问题
是否有一种巧妙的方法来添加 arrays/vectors 不具有相同列长度的列,只需将零添加到太短的列中?我有 additions/subtractions 包含多个变量。
这样:
a=[ 1; 2; 3]; b=[1;5]
a+b=[2; 5; 3]
或:
a-b=[0;-3;3]
而不是:
Error using +
Matrix dimensions must agree.
我做了什么
b(numel(a),1) = 0;
如果添加一些变量,这很好用,但是在重复多个变量时这会很烦人,尤其是当您不知道哪个列最长时。因此,问题是是否有更简单快速的方法来添加不同的列长度。
编辑
真正的问题是,如果有超过 "just a few" 个变量的自动化方法。
a=[ 1; 2; 3]; b=[1;5];
if numel(b)~=numel(a)
if numel(b)<numel(a) % If b is shorter, extend it
b = [b; zeros(numel(a)-numel(b),1)];
else % If a is shorter, extend it
a = [a; zeros(numel(b)-numel(a),1)];
end
end
a+b
您或多或少需要手动完成。例如:
s = [];
s(1,1:numel(a)) = a;
s(2,1:numel(b)) = b; % assigning a row (or column) automatically pads with zeros
% if needed. This works irrespective of which is bigger, a or b
s = sum(s,1); % sum along each column
如果您有几个变量,最好将它们放在元胞数组中,这样您就可以遍历它们:
c = {[1; 2; 3] [1;5] [10 20 30 40]}; % cell array of all "variables"
s = [];
for k = 1:numel(c);
s(k,1:numel(c{k})) = c{k}; % this zero-pads preceding cells' contents if needed
end
s = sum(s,1); % sum along each column
以上可能会很慢,因为 s
是动态重新分配的。您可以按如下方式预分配:
c = {[1; 2; 3] [1;5] [10 20 30 40]}; % cell array of all "variables"
m = max(cellfun(@numel, c)); % maximum vector size
s = zeros(numel(c), m); % preallocate and initiallize to zeros
for k = 1:numel(c);
s(k,1:numel(c{k})) = c{k}; % some entries maybe be left as zero
end
s = sum(s,1); % sum along each column
A=[1, 2, 3]; B=[1,5];
[A,zeros(1,length(B)-length(A))]+[B,zeros(1,length(A)-length(B))]
ans =
2 7 3
[A,zeros(1,length(B)-length(A))]-[B,zeros(1,length(A)-length(B))]
ans =
0 -3 3
将它们粘贴到函数中即可完成
问题
是否有一种巧妙的方法来添加 arrays/vectors 不具有相同列长度的列,只需将零添加到太短的列中?我有 additions/subtractions 包含多个变量。
这样:
a=[ 1; 2; 3]; b=[1;5]
a+b=[2; 5; 3]
或:
a-b=[0;-3;3]
而不是:
Error using + Matrix dimensions must agree.
我做了什么
b(numel(a),1) = 0;
如果添加一些变量,这很好用,但是在重复多个变量时这会很烦人,尤其是当您不知道哪个列最长时。因此,问题是是否有更简单快速的方法来添加不同的列长度。
编辑
真正的问题是,如果有超过 "just a few" 个变量的自动化方法。
a=[ 1; 2; 3]; b=[1;5];
if numel(b)~=numel(a)
if numel(b)<numel(a) % If b is shorter, extend it
b = [b; zeros(numel(a)-numel(b),1)];
else % If a is shorter, extend it
a = [a; zeros(numel(b)-numel(a),1)];
end
end
a+b
您或多或少需要手动完成。例如:
s = [];
s(1,1:numel(a)) = a;
s(2,1:numel(b)) = b; % assigning a row (or column) automatically pads with zeros
% if needed. This works irrespective of which is bigger, a or b
s = sum(s,1); % sum along each column
如果您有几个变量,最好将它们放在元胞数组中,这样您就可以遍历它们:
c = {[1; 2; 3] [1;5] [10 20 30 40]}; % cell array of all "variables"
s = [];
for k = 1:numel(c);
s(k,1:numel(c{k})) = c{k}; % this zero-pads preceding cells' contents if needed
end
s = sum(s,1); % sum along each column
以上可能会很慢,因为 s
是动态重新分配的。您可以按如下方式预分配:
c = {[1; 2; 3] [1;5] [10 20 30 40]}; % cell array of all "variables"
m = max(cellfun(@numel, c)); % maximum vector size
s = zeros(numel(c), m); % preallocate and initiallize to zeros
for k = 1:numel(c);
s(k,1:numel(c{k})) = c{k}; % some entries maybe be left as zero
end
s = sum(s,1); % sum along each column
A=[1, 2, 3]; B=[1,5];
[A,zeros(1,length(B)-length(A))]+[B,zeros(1,length(A)-length(B))]
ans =
2 7 3
[A,zeros(1,length(B)-length(A))]-[B,zeros(1,length(A)-length(B))]
ans =
0 -3 3
将它们粘贴到函数中即可完成