0

Ok this is what I did in Matlab to form the matrix A1 from matrix A

 x=1:10;

 x=x';
 A=toeplitz(x,[x(1);flip(x(2:end))]);

 A1=zeros(10,5);
 count=0;

 for i=1:2:10
 count=count+1;
 A1(:,count)=A(:,i);
 end

The output is as shown

 >> A

 A =

 1    10     9     8     7     6     5     4     3     2
 2     1    10     9     8     7     6     5     4     3
 3     2     1    10     9     8     7     6     5     4
 4     3     2     1    10     9     8     7     6     5
 5     4     3     2     1    10     9     8     7     6
 6     5     4     3     2     1    10     9     8     7
 7     6     5     4     3     2     1    10     9     8
 8     7     6     5     4     3     2     1    10     9
 9     8     7     6     5     4     3     2     1    10
10     9     8     7     6     5     4     3     2     1

>> A1

A1 =

 1     9     7     5     3
 2    10     8     6     4
 3     1     9     7     5
 4     2    10     8     6
 5     3     1     9     7
 6     4     2    10     8
 7     5     3     1     9
 8     6     4     2    10
 9     7     5     3     1
10     8     6     4     2

The same logic I want in r(i showed only code related to for loop as the rest of the things i.e. initial assignment is the same as Matlab code mention above ), and I tried following in r but it is showing an error. I am a beginner in r programming & need help in the assignment operation of columns of the matrix A to other columns of the matrix A1(empty matrix).

for(i in seq(from=1, to=10, by=2)){
 count=count+1
 A1[c(1:10),(1:count)]=A[C(1:10),(1:i)]    
 }

 Error in C(1:10): object not interpretable as a factor

In the resultant matrix, I do not want to select particularly odd or even columns of the orignal matrix. but The first column of matrix A1 and matrix A should be the same and the rest of the columns of A1 I can select by changing the step size as shown below in Matlab code

 A1 = A(:,1:3:10);    

>> A1

A1 =

 1     8     5     2
 2     9     6     3
 3    10     7     4
 4     1     8     5
 5     2     9     6
 6     3    10     7
 7     4     1     8
 8     5     2     9
 9     6     3    10
10     7     4     1

  A1 = A(:,1:4:10);    

>> A1

 A1 =

 1     7     3
 2     8     4
 3     9     5
 4    10     6
 5     1     7
 6     2     8
 7     3     9
 8     4    10
 9     5     1
10     6     2
Anil Sarode
  • 175
  • 13
  • Hi Anil! Welcome to Stack Overflow. You don't need that loop in the Matlab code. Just do `A1 = A(:,1:2:10);`. You can probably do something similar in R. – Andrew Janke Apr 21 '19 at 12:53
  • You used a capital `C` instead of a lower case `c`. `C(1:10)` should read `c(1:10)`. – Dan Apr 21 '19 at 12:54
  • @AndrewJanke thanks for one line answer for Matlab code. – Anil Sarode Apr 21 '19 at 13:04
  • In R, you could do `A[,seq(2, ncol(A), 2)]` or `A[, c(FALSE, TRUE)]` – Ronak Shah Apr 21 '19 at 13:06
  • @Lyngbakr by using small c at least, I am able to access all row and a particular column (or simply column) but not able to assign to another column of a matrix A1 inside the loop. But by using capital C not able to access the column of first matrix A. – Anil Sarode Apr 21 '19 at 13:10

0 Answers0