MATLAB Help in MATLAB, plotting a signal

AI Thread Summary
The discussion centers around a MATLAB user encountering an error when attempting to plot a signal. The user initially tries to compute the signal using standard multiplication, which leads to a matrix dimension error. The key issue is the use of the "*" operator for matrix multiplication instead of element-wise multiplication. A solution is provided, correcting the expression to use the ".*" operator for element-wise multiplication and ensuring that the random noise is generated with the same size as the time vector. The corrected code successfully resolves the error, allowing the user to plot the signal without issues.
Az83
Messages
7
Reaction score
0
I'm pretty new in MATLAB, and I can't seem to get it to display a signal. I have the following:

t=1:1:100;
s= 2*sin(2*pi*.05*t)*5*sin(2*pi*2*t)+.75*randn;
plot(t,s)


But i keeps giving me the following error:

? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> signal at 5
s= 2*sin(2*pi*.05*t)*5*sin(2*pi*2*t)+.75*randn;

Does anyone have any suggestions?
 
Physics news on Phys.org
one of the things about MATLAB is that the "*" symbol means matrix multiplication, but has been allowed to be used when either the left or right operand (or both) are scalers (which, in MATLAB is a 1x1 matrix, but they recognize that and assign meaning to a multiplication of that as a scaler against any matrix). but what you can't do is multiply two matrices together (bigger than the 1x1, which is dually interpreted as a scaler), unless the number of columns of the left operand is equal to the number of rows of the right. to do element-by-element multiplication of two identically sized and shaped arrays (i won't call them "matrices" here), you use the ".*" operator. sometimes you'll also need to use the ".^" operator, too, because the normal power operator "^" also has matrix meaning when one of the operands are matrices.
oh, i forgot to correct the expression:

s= (2*sin(2*pi*.05*t)) .* (5*sin(2*pi*2*t)) +.75*randn(size(t));

i think that will work.
 
Last edited:
That worked perfectly. Thanks so much for the help and quick reply :-)
 

Similar threads

Replies
10
Views
3K
Replies
8
Views
2K
Replies
1
Views
2K
Replies
4
Views
3K
Replies
4
Views
4K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
2
Views
3K
Back
Top