필터 지우기
필터 지우기

Convolution of matrix rows with while loop

조회 수: 3 (최근 30일)
Calle Swedhag
Calle Swedhag 2016년 11월 30일
답변: Alexandra Harkai 2016년 12월 1일
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

답변 (1개)

Alexandra Harkai
Alexandra Harkai 2016년 12월 1일
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!

Translated by