the variable appears to change size of every loop iteration?
1 次查看(过去 30 天)
显示 更早的评论
I tried to fix this problem but error message appear In an assignment A(I) = B, the number of elements in B and I must be the same
What is wrong with this code?
D=zeros(4)
For i=1:4
D(i)=conv(D(i,:),[1 0])
End
0 个评论
回答(1 个)
Mark Thompson
2020-8-6
Hi msh,
Your variable D is a 4 x 4 matrix. And you have a 1 x 2 value to convolve with it. Referring to the documentation for the conv() function if you want a full length convolution result, thenthe result will be a 4 x 5 matrix. So, your variable for storing the result of each convolution will need to be this size (i.e. convolving a 1 x 4 with a 1 x 2 results in a 1 x 5 result). I try to explain in the code and comments below:
D = zeros(4);
val = [1 0];
convolutionLength = length(D) + length(val) - 1; % number of values returned as a result of the convolution
result = zeros(length(D),convolutionLength);
for i=1:4
result(i,:) = conv(D(i,:),val);
end
Also worth noting is your assignment to D(i) in the for loop will only ever access the element at D(i,1). So you're trying to store 5 values into 1! Test this by assigning D = zeros(4) in your command window and then typing D(1) and look at the result.
If you really want to overwrite your D variable with the convolution result then you can set the "shape" flag in the conv() function to 'same'. e.g. conv(u,v,'same'); this will return a result the same length as variable u. In your case you could use this as in the following code snippet:
D = zeros(4);
for i=1:4
D(i,:) = conv(D(i,:),[1 0],'same');
end
I hope this helps!
4 个评论
Mark Thompson
2020-8-6
D = zeros(4); is re-initialising D to be a 4 x 4 matrix of 0's. So if you are doing this after D=[D1;D2;D3;D4] then you are erasing that data and writing the 4 x 4 matrix of 0's to the variable D.
I can't help anymore unless you can post more of your code, sorry. What data types are D1, D2, D3 and D4?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!