Convolution of matrix rows with while loop

3 views (last 30 days)
I want a while loop to execute convolutions on top of eachother until the loop limit is reached. t_mat is a matrix and the different t_tot's are vectors produced by convolution of the matrixs' rows. In the end I should end up with a vector t_tot10. Can someone help my write a loop for getting to the vector t_tot10. I'm asking for a loop because in my assignment I will need a t_tot10000.
LOOP_LIMIT = 10
while (k <= 10 && LOOP_LIMIT > 0)
t_tot1 = conv(t_mat(k,:), t_mat(k+1,:));
t_tot2 = conv(t_tot1,t_mat(k+2,:);
t_tot3 = conv(t_tot2,t_mat(k+3, :);
LOOP_LIMIT = LOOP_LIMIT - 1;
end

Answers (1)

Alexandra Harkai
Alexandra Harkai on 1 Dec 2016
Avoiding the var1,var2, etc. naming is good practice. In case you only need the last one, it would be fairly simple. This would execute LOOP_LIMIT-1 convolutions on top of each other, applying rows of t_mat one after the other.
LOOP_LIMIT = 10;
k = 2;
t_tot = t_mat(1,:); % Initialise t_tot for k=1
while k <= LOOP_LIMIT
t_tot = conv(t_tot, t_mat(k, :));
k = k + 1;
end
You could use a 'for' loop instead of a 'while' loop:
LOOP_LIMIT = 10;
t_tot = t_mat(1,:); % Initialise t_tot for k=1
for k = 2 : LOOP_LIMIT % Go through each row, starting from the second
t_tot = conv(t_tot, t_mat(k, :));
end

Community Treasure Hunt

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

Start Hunting!