Error: In an assignment A(:) = B, the number of elements in A and B must be the same.

3 次查看(过去 30 天)
I have been trying to get all the code to run but it keeps giving me the same error "In an assignment A(:) = B, the number of elements in A and B must be the same".
I managed to calculate w but the operations below are not performed automatically even though I already have the variables I need.
Please, help me!
A=[-1 -2 1 2;-2 3 0 -2;1 0 2 1;2 -2 1 4];
n=4;
v=eye(n,n);
HAH=A;
for k=1:n-2
x=A(:,n-k+1);
a2=x.^2;
alpha2=sum(a2(1:n-k));
sg=-sign(x(n-k));
alpha=sg*sqrt(alpha2);
w=zeros(n-k+1,1);
for i=1:n-k-1
w(n-k)=sqrt((1/2)*(1-(x(n-k)/alpha)));
wnk=w(n-k);
W(i)=-((1/2)*(x(i)/alpha*wnk));
end
w(i)=w;
end
Unable to perform assignment because the left and right sides have a different number of elements.
u=HAH*w;
h=w.'*u;
q=u-(h*w);
HAH=HAH-2*w*q.'-2*q*w.';
H=eye(length(w*w.'))-2*w*w.';
v=v*H;

回答(1 个)

Torsten
Torsten 2023-6-22
w(i)=w;
This line of code makes no sense. You want to assign a vector w to a scalar w(i).

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by