the variable appears to change size of every loop iteration?

6 Ansichten (letzte 30 Tage)
msh jgtu
msh jgtu am 5 Aug. 2020
Kommentiert: Mark Thompson am 6 Aug. 2020
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

Antworten (1)

Mark Thompson
Mark Thompson am 6 Aug. 2020
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 Kommentare
Mark Thompson
Mark Thompson am 6 Aug. 2020
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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by