Nested loop related issue

1 view (last 30 days)
Raisul Islam
Raisul Islam on 25 Oct 2017
Edited: per isakson on 6 Nov 2017
Impulse = 961 by 1 double
residual = 5142 by 32 double
.
impulse=impulse(:);
residual=v;
[m,n]=size(v);
HD=[];
%
for i=1:m
for j=1:n
HD = impulse(:).*v(i);
end
end
GHD=zscore(HD);
The problem is with the for loop I get the output for the first column only. So I get HD= p by 1 matrix. I want HD =p by 32 matrix. Currently, my impulses elements are multiplied with elements with the rows for the first column only. What modification should I do here to have all elements of impulse to be multiplied with all other columns just as it does for the first column in this loop. So I just need the same operation executed across all columns of v. What might be the nested loop? Should I use another for loop or a while loop inside..I appreciate your skilled solutions. As you see I am a new user, you are helping a lot.
  3 Comments
Raisul Islam
Raisul Islam on 25 Oct 2017
Hi KL, the loop helped. it allowed me to multiply all elements in impulse with v and the first column that is produced is as expected. However, I will still appreciate your suggestion on matching the dimensions and i would like to try them. How can I have the same operation across columns. Thank you again and again.
Raisul Islam
Raisul Islam on 26 Oct 2017
KL, I tried reshaping, repeatmat, meshgrid...nothing helped. I just need the loop to return me 31 columns instead of one. My rows are 2883 which equals to rows of impulse. Not the rows of v. The output should have at least = size (v)

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 25 Oct 2017
SO many problems. First of all impulse was already converted to a column vector with your first line of code so you no longer need the(:) in
HD = impulse(:).*v(i);
Secondly, v is a 2-D array and takes two indexes. Since you only supplied 1 it's acting as a linear index and will go down rows first, but won't include the whole array, it will just have the first (badly-named) n elements (which is really columns) in the first column, which has m rows. Since m and n may not match up, you will either take less than the first column, or take more, in which case it will start to take some from the second column.
So this is better but I still can't figure out what you want to do.
% Turn impulse into a 961 by 1 column vector
impulse = impulse(:);
residual = v; % 5142 by 32
[rows, columns] = size(v);
HD = []; % ??????
for row = 1 : rows
for column = 1 : columns
HD = impulse(:) .* v(row, column);
end
end
GHD = zscore(HD);
Exactly what rows and columns of what do you want to multiply by the rows and columns of what else? I can't figure it out from your strange code. What dimensions do you expect HD to be when everything is all done? 961 is not an integer multiple of either 5142 or 32, so something is not going to align up right.
Third, you define residual but never use it - you continue to use v instead. Why?
  4 Comments
Raisul Islam
Raisul Islam on 26 Oct 2017
Thank you. I am doing simple historic decomposition. The idea behind is that the vector called impulse is coming from impulse response function. The matrix called residual is coming from least square analysis. What you want to do here is that check the impact of the entire impulse vector/matrix on all elements of residual. For example if you have two variables , then you have two residual series. Impulse can be of any size depending on your lag size, AR or MA sizes. I am trying to have all elements of impulse multiplied with each element of residual and then adding them up. The multiplication and summation happens for each cell of residual. Now you want that to happen for all cells in your residual matrix. This is in a nutshell. To understand the steps before is a lot. OLS, transformation,Function calculation, trimming, and this is the last step.......
Raisul Islam
Raisul Islam on 26 Oct 2017
To be simple, forget about my column loop. I might made a mistake. The row loop helps doing what I want across all rows for just the first series. Now what additional statement shoukd I add so that operation that works for the rows in the first series happens for all other columns. I am supposed to have a n by p matrix in HD. I am getting a n by 1 matrix. That’s the problem.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!