MATLAB Answers

How to use for loop correctly and indexing my result?

4 views (last 30 days)
Eric Chua
Eric Chua on 31 May 2020
Commented: Eric Chua on 31 May 2020
a = [1 1 1 0 0 0 0 1 0 1 0 1 1 0 0 1] ;
b = [1 1 1 0 1 0 0 0 1 1 1 0 0 0 0 1] ;
S = [a, a, a, a, a, a, a, a, a ,a;b, b, b, b, b, b, b, b, b, b];
x1 = S(1:1,:);
x2 = S(2:2,:);
L = 3;
for i = 3:160
x11 = [x1(i),x2(i)]
x12 = [x1(i-1),x2(i-1)]
x13 = [x1(i-L+1),x2(i-L+1)]
x14 = [1]
X1 = [x11 x12 x13 x14]
end
Hi this is my code. After I run it, it display the answer for x11, x12, x13, x14, and X1 directly with i = 3 to 160, a total of 158 sets of results. How to code such that i want to select my 3rd result to show only?

  0 Comments

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 31 May 2020
Use ; at the end of each line inside the loop.
X1(3,:) % after the loop

  6 Comments

Show 3 older comments
Eric Chua
Eric Chua on 31 May 2020
Im still fresh to coding and im learning. Can you please help?
madhan ravi
madhan ravi on 31 May 2020
Please illustrate with a short example with an expected result, so it's easy to understand the main goal.
Eric Chua
Eric Chua on 31 May 2020
I have training sequence a and b with length of 16 binary element each. The S I have defined is to concatenate the sequence by itself for 10 times. So my new 'a ' will be a 1x160 matrix/row vector, and similar to b, which i have defined as x1(for new 'a') and x2(for new 'b'). For example if I concatenate the a by itself for one time,
the new a = [1 1 1 0 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 0 1 0 1 0 1 1 0 0 1]
From the above posted question, X1 is made up of three 1x2 vector (x11, x12, and x13) and a 1x1 vector (a noise which will remain at 1 as the last element of X1)
For example, for i = 3,
x11 = [1 1] (the first element here is from the third element of the new 'a' and second element here is from
the third element of the new 'b')
x12 = [1 1]
x13 = [1 1]
x14 = [1]
so, my X3 = [1 1 1 1 1 1 1]
For i = 4,
X11 = [0 0]
X12 = [1 1]
X13 = [1 1]
X14 = [1]
so, my X4 = [0 0 1 1 1 1 1]
Im wondering how to code this and when I want my X150 for example, I can write it in the command window and get the vector of X150.
Hope you can understand my questions, much thanks!

Sign in to comment.


Translated by