jeudi 23 mars 2017

Creating new matrix from combining two other by using an if-statement

I have a question hoping to get some help out there. I'll try to state my problem as clear as possible. However, I am still a beginner when it comes to Matlab so please forgive any misleading descriptions. ;)

So I have a matrix in dimension 8x6. Half of the elements in this matrix are 0, which is totally fine. Now, I would like to refer to another matrix which is 160x6. The 8x6 matrix is based on the 160x6 matrix and results from a rolling window (20 observations).

I would like to create a new matrix (again 160x6). Whenever an element in my 8x6 matrix equals 0, I would like the 20 observations from the original 160x6 matrix referring to this element (being 0) to be 0 as well.

I have tried the following:

for t=1:T
    for i=1:N
    if B(:,i) == 0;
    C(t,i) = 0;
    else
    C(t,i) = A(t,i);
    end
    end
end

where I have:

"A" being the 160x6 matrix

"B" being the 8x6 matrix

"C" being the new output as 160x6 matrix

At the moment, I obtain a "new" 160x6 matrix (C), but it exactly replicates the original 160x6 matrix (A). So the looping or the if statement is incorrect.

I really appreciate your comments!

Joe

btw: Please apologize the messy description of my problem ;)

Aucun commentaire:

Enregistrer un commentaire