英文:
Finding two Vectors
问题
我有一个大小为100x1的向量S
。使用S
创建子向量A
和B
,例如A=S(1:50)
和B=S(5:55)
。注意A
和B
中元素的偏移。生成一个结果向量R=a*A + b*B
。
现在问题是,已知a
、b
、R
和A
与B
之间的偏移,如何计算A
和B
?
英文:
I have a vector S
of size say 100x1. Sub-vectors A
and B
are created using S
, say A=S(1:50)
and B=S(5:55)
. Note the shift in the elements in A
and B
. A resultant vector is generated R=a*A + b*B
.
Now the problem is a
, b
, R
and the shift between A
and B
are given. How to calculate A
and B
?
答案1
得分: 3
你有一个欠定的方程系统,所以无法得到精确的结果。但Matlab完全能够估计这种方程系统的解决方案:
% 定义参数
start = 10;
offset = 2;
len = 20;
a = 5;
b = 25;
% 从随机数组S创建A和B
S = randi(30,1,100);
A = S(start:start+len-1);
B = S(start+offset:start+offset+len-1);
% 计算R
R = a*A + b*B;
% 创建我们的线性方程系统
M = cat(2,diag(ones(len,1)*a),zeros(len,offset)) + cat(2,zeros(len,offset),diag(ones(len,1)*b));
% 解决方程系统
S_theta = (M\R.').';
% 比较S_theta和S
S_theta(offset+1:end) - B
结果:
请注意,如果满足以下条件,结果会更好:
- 偏移量小(方程系统中未知数较少)
- a和b之间的差异大。
当我们执行:
M\R.'
Matlab解决方程系统 Mx = R
,由于M
是欠定的(我们有len
个方程和len+offset
个未知数),Matlab使用QR Solver来找到最小范数残差解决方案,类似于Ax = B
类型的问题。
英文:
You have an underdetermined system of equation. So you cannot get the exact result. But matlab is totally able to estimate the solution of such a system of equation:
% Define the parameters
start = 10;
offset = 2;
len = 20;
a = 5;
b = 25;
% Create A and B from a random array S.
S = randi(30,1,100);
A = S(start:start+len-1);
B = S(start+offset:start+offset+len-1);
% We compute R
R = a*A+b*B;
% We create our system of linear equation:
M = cat(2,diag(ones(len,1)*a),zeros(len,offset))+cat(2,zeros(len,offset),diag(ones(len,1)*b));
% We solve the system
S_theta = (M\R.').'
Let's compare S_theta to S:
S_theta(offset+1:end)-B
Result:
Noticed that the result will be better if:
- The offset is small (less unknows in the system of equation)
- The difference between a and b is big.
When we do:
M\R.'
Matlab solve the system Mx = R
, since M
is underdetermined (we have len
equations for len+offset
unknows) then matlab use a QR Solver
that find minimum-norm-residual solution to Ax = B
type of problem.
答案2
得分: 0
您基本上有一个具有变量数量等于numel(R)+shift
和等式数量等于numel(R)
的线性系统。我认为不可能得到您想要的结果,因为未知变量始终多于等式数目:
% 第一组变量
R(1) = a*S(1)+b*S(5); % 一个等式,两个未知数
R(5) = a*S(5)+b*S(9); % 两个等式,三个未知数
R(9) = a*S(9)+b*S(13); % 三个等式,四个未知数
% 第二组变量
R(2) = a*S(2)+b*S(6); % 四个等式,六个未知数
R(6) = a*S(6)+b*S(10); % 五个等式,七个未知数
% 依此类推
请注意,这些变量和等式之间的数量关系可能会导致无法唯一解决系统。
英文:
You essentially have a linear system with a number of variables equaling numel(R)+shift
and a number of equations equaling numel(R)
. I don't think it's possible to get what you want, since there are always more unknown variables than you have equations:
% First chain of variables
R(1) = a*S(1)+b*S(5); %One equation, two unknowns
R(5) = a*S(5)+b*S(9); %Two equations, three unknowns
R(9) = a*S(9)+b*S(13); %Three equations, four unknowns
% Second chain of variables
R(2) = a*S(2)+b*S(6); %Four equations, six unknowns
R(6) = a*S(6)+b*S(10); %Five equations, seven unknowns
% etc.
答案3
得分: -2
在字符串S的前面(或后面)添加shift
个零,将前面的部分乘以a
,后面的部分乘以b
,然后将它们相加。这将问题简化为子串搜索。你可以采用蛮力搜索或使用更复杂的方法;请参考以下链接:
https://en.wikipedia.org/wiki/String-searching_algorithm
英文:
Slap shift
zeroes on the front (resp. back) of S, multiply the former by a
and the latter by b
, and add them together. This reduces the problem to a substring search. You can either brute force that or use something more sophisticated; see
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论