Nested loop related issue
이전 댓글 표시
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
KL
2017년 10월 25일
Hi Raisul,
here you're writing two loops but you're only using 'i' inside but not 'j', that's why it doesn't work as you expect it to be. I would recommend you to read about indexing on the following link: https://de.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html.
Coming to your question, firstly if impulse is already a column vector, writing
impulse = impulse(:);
doesn't really do anything. So you don't need it.
From your code, it looks like, this time you're trying to multiply impulse across all columns of v. But your v has 5142 rows while impulse has only 961 rows. How do you expect them to be multiplied then?
Raisul Islam
2017년 10월 25일
Raisul Islam
2017년 10월 26일
답변 (1개)
Image Analyst
2017년 10월 25일
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
Raisul Islam
2017년 10월 25일
편집: Raisul Islam
2017년 10월 25일
Image Analyst
2017년 10월 26일
I honestly have no idea what you want to do. Do you have a mathematical formula (a picture) that shows what operation you are trying to do digitally?
Raisul Islam
2017년 10월 26일
Raisul Islam
2017년 10월 26일
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!