# implement the A matrix using for loop

26 views (last 30 days)
Chiu tik hong on 9 Apr 2021 at 13:11
Edited: David Hill on 9 Apr 2021 at 14:09
Given two 1D-vectors:
x = [286; 206; 191; 487; 510];
y = [93; 523; 333; 494; 221];
how to implement the A matrix using for loop and using the 2 vetcors?
matrix A should be the same as below:
A(:,1)=[286;0;206;0;191;0;487;0;510;0];
A(:,2)=[93;0;523;0;333;0;494;0;221;0];
A(:,3)=[1;0;1;0;1;0;1;0;1;0];
A(:,4)=[0;286;0;206;0;191;0;487;0;510];
A(:,5)=[0;93;0;523;0;333;0;494;0;221];
A(:,6)=[0;1;0;1;0;1;0;1;0;1];
David Fletcher on 9 Apr 2021 at 13:53
So, what have you tried? I assume that you can see the pattern relating the input to the output?

David Hill on 9 Apr 2021 at 14:01
Edited: David Hill on 9 Apr 2021 at 14:09
Why do you need a for-loop? Looks like you have the idea.
A=zeros(2*length(x),6);
A(1:2:end,1)=x;
A(1:2:end,2)=y;
A(1:2:end,3)=1;
A(2:2:end,4)=x;
A(2:2:end,5)=y;
A(2:2:end,6)=1;
You could
A=zeros(2*length(x),6);
X=[ones(length(x),1),x,y];
for k=1:6
if k<=3
A(1:2:end,k)=X(:,mod(k,3)+1);
else
A(2:2:end,k)=X(:,mod(k,3)+1);
end
end