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

1 次查看(过去 30 天)
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?

采纳的回答

DJ V
DJ V 2016-12-6
Received an answer elsewhere.

更多回答(1 个)

Adam
Adam 2016-12-6
编辑:Adam 2016-12-6
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by