MATLAB Answers

How to do matrix indexing using for loop that involved sampling at time k?

4 views (last 30 days)
Eric Chua
Eric Chua on 29 May 2020
Commented: Gaurav Garg on 12 Jun 2020
%Designed training sequences x1 and x2
x1 = [1,1,1,0,0,0,0,1,0,1,0,1,1,0,0,1] ;
x2 = [1,1,1,0,1,0,0,0,1,1,1,0,0,0,0,1] ;
x [k] = [ x1[k] , x2[k], ... xM[k] ]^T
X [k] = [x^T[k], x^T[k-1], ..., x^T[k-L+1] , 1]^T
How do i create a matrix x[k] and X[k] that depend on the time k using Matlab? I have trouble indexing such complex vector or matrix. For example, k is in the range of 3<= k <= 16 . For example, x[3] = [ x1[3], x2[3] ]^T which is x3 = [ 1, 1]^T. Here x1[3] means the 3rd element of vector x1 which is the number 1, x2[3] mean the 3rd element of vector x2 which is also a number,1.
x[4] = [ x1[4], x2 [4] ] refer as x[4] = [ 0 , 0 ] so on and so forth
then X [3] will be : X [3] = [ x1[3], x2[3], x1[2], x2[2], x1[1], x2[2], 1 ]^T which is X [3] = [1, 1, 1, 1, 1, 1, 1]^T , the ^T here means tranpose of the vector.


Sign in to comment.

Accepted Answer

Gaurav Garg
Gaurav Garg on 1 Jun 2020
Hey Eric
Assuming that you have 2 arrays to start with (x1 and x2, as given in your question), you can declare an empty matrix x (16 x 2), and assign the values as here:
x = zeros(16,2)
for i=1:16
x(i,:)=[x1(i) x2(i)]
For kth element, x(k) = [x1(k) x2(k)].
For X matrix, you will have to declare the matrix with variable dimensions:
X = cell(16,1)
X{1} = [x1(1) x2(1)]
for i=2:16
X{i} = [x1(i) x2(i) X{i-1}]
Here, X{k} = [x1(k) x2(k) x1(k-1) x2(k-1).... x2(1)].
If you have more arrays (x1, x2, x3,...., xm), you can loop in the inner values.
After obtaining the matrices, you can transpose each value (x(k) or X(k)) to get the final answer.


Eric Chua
Eric Chua on 7 Jun 2020
If you have more arrays (x1, x2, x3,...., xm), you can loop in the inner values. May I know how to do this? Lets say if M = 3, then my x and X will have more elements in it. For example, x = [x1(1) x2(1) x3(1)] and
X = [x1(1) x2(1) x3(1) x1(k-1) x2(k-1) x3(k-1) x1(k-L+1) x2(k-L+1) x3(k-L+1) 1]
Gaurav Garg
Gaurav Garg on 12 Jun 2020
Rather than naming you arrays x1,x2,....., you might have to take a 2-D array and then loop over the entries.
Let's say A = [ x1
A is a matrix where x1,x2,x3 are rows (x1,x2,x3 are vectors (as were in your case).
You can loop over rows (for each array) and over columns (for each entry in array) and use the same algorithm as has been answered.
% Assuming you have filled array A as told above
for i=1:16
for j=1:3 % using 3 as you have 3 arrays - x1,x2,x3
x(i,:)=[x(i,:) A(j,i)] % A(j,i) represent jth array (xj), ith entry (xj())

Sign in to comment.

More Answers (0)

Translated by