1

See the below code:

clc;
clear;


%rng('default')
t = 1900;
u  = randn(t+100,1);
e  = randn(t+100,1);
ep = randn(t+100,1);
A = 0; 
x=zeros(t+100,1);
for iii = 1:5000

   for i=2:t+100 
      x(i) = 0.99*x(i-1)+u(i);
      u(i) = 0.5*u(i-1)+e(i);
      ep(i)=0.99*u(i);
      y(i) = A*x(i-1)+ep(i);

end

y is a 1 x 2000 but I want to be a 2000 x 1, I used y' but this doesnt seem to work when I perform later operations on y. My MATLAB seems to flip between a 1 x 2000 and a 2000 x 1

Thanks in advance

am304
  • 13,758
  • 2
  • 22
  • 40
user30609
  • 264
  • 1
  • 17
  • You can use the semi colon operator to ensure that `y` will be a column vector: `y = y(:)`. Also noticed that `'` is the conjugate transpose operator, you probably want to use `.'`. More information [here](https://stackoverflow.com/questions/25150027/using-transpose-versus-ctranspose-in-matlab) – obchardon Jul 31 '19 at 14:19

1 Answers1

4

First: you are missing an end for the second for loop. I am not sure what the iii loop is doing anyway because iii is not used anywhere.

Second, and to answer your question, you can (and probably should) pre-allocate your variables before your loop. This will not only allow you to specify whether each variable is a row or column vector, but it will also improve the speed and efficiency of your code.

So, corrected version would be:

clc;
clear;

%rng('default')
t = 1900;
u  = randn(t+100,1);
e  = randn(t+100,1);
ep = randn(t+100,1);
A = 0; 
x = zeros(t+100,1);
y = zeros(size(x));

for i=2:t+100

   x(i) = 0.99*x(i-1)+u(i);
   u(i) = 0.5*u(i-1)+e(i);
   ep(i)=0.99*u(i);
   y(i) = A*x(i-1)+ep(i);

end

And just to check y is of the correct size after running the code:

>> whos y
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        y        2000x1                      16000  double

Total is 2000 elements using 16000 bytes
am304
  • 13,758
  • 2
  • 22
  • 40