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

조회 수: 3 (최근 30일)
%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.

채택된 답변

Gaurav Garg
Gaurav Garg 2020년 6월 1일
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)]
end
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}]
end
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.
  댓글 수: 3
Eric Chua
Eric Chua 2020년 6월 7일
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 2020년 6월 12일
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
x2
x3]
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.
Pseudo-code:
% 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())
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by