I have a matrix A in Matlab of dimension Nx(N1) , e.g.
N=5;
A=[1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16;
17 18 19 20];
I want to rearrange the elements of A in a certain way. Specifically I want to create a matrix B of dimension (N1)xN such that:
for i=1,...,N ,
B(:,i) collects
1) the first i1 elements of the i1 th column of A and
2) the last Ni elements of the i th column of A .
Notice that for i=1 the i1 th column of A does not exist and therefore 1) is skipped; similarly, for i=N thei th column of A does not exist and therefore 2) is skipped.
In the example above
B=[5 1 2 3 4
9 10 6 7 8
13 14 15 11 12
17 18 19 20 16];
This code does what I want. I am asking your help to vectorise it in an efficient way.
B=zeros(N1,N);
for i=1:N
if i>1 && i<N
step1=A(1:i1,i1);
step2=A(i+1:N,i);
B(:,i)=[step1;step2];
elseif i==1
B(:,i)=A(i+1:N,i);
elseif i==N
B(:,i)=A(1:i1,i1);
end
end
