I'm getting an error in the line with product (count)= A*B/C

1 visualizzazione (ultimi 30 giorni)
function [ out ] = triangle_wave( n )
%TRIANGLE_WAVE Summary of this function goes here
% Detailed explanation goes here
t = 4*pi/1001;
tvector = 0:t:4*pi;
for count = 1:n+1
A= -1^(count-1);
B = sin((2*(count-1)+1)*tvector);
C = (2*(count-1)+1)^2;
product(count) = A*B/C;
end
for count =0:n
out = out + product(count)
end
end
I get an error in line 10. IT says subscripted assignment dimension mismatch. What does this mean?

Risposta accettata

DJ V
DJ V il 6 Dic 2016
Received an answer elsewhere.

Più risposte (1)

Adam
Adam il 6 Dic 2016
Modificato: Adam il 6 Dic 2016
It means that
size( product(count) )
does not evaluate to the same thing as
size( A*B/C )
so trying to assign the latter into the former gives a dimension mismatch. product doesn't appear to be predefined anywhere so unless A*B/C results in a scalar this will not work.
e.g. the following code, in a completely clean workspace gives this error too:
a(9) = [3; 4];
because a does not exist and therefore a(9) will be assumed to be scalar if it were to be created at this moment. [3; 4] obviously is not scalar.
a(9) = 3;
works fine and creates a row vector of 8 zeros preceding the 3;
If you are trying to assign multiple dimensions to a result then you should have multiple dimensions on the left-hand side of the = also in general.
  1 Commento
DJ V
DJ V il 6 Dic 2016
I fixed the output and simplified it. It is still producing the wrong answer.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by