Error- using --- .* -- Matrix dimensions must agree.

Hello everyone I am new to Matlab and have a simple question. The error I am receiving is one that I don't fully understand. I have tried changing the .* to a * in my equation but still come up with the same error. I am sure it is something that I am completely overlooking and cant figure it out any help would be appreciated. My error is in x = Xk .* cos((fk * pi) .* t);
function [x,t] = syn_sin(fk,Xk,fs,dur, tstart)
%SYN-SIn FUnction to sythesize a sum of cosine waves
% Usage:
% [xx,tt] = syn_sin(fk,Xk,fs,dur,tstart)
% fk = vector of frequencies
% (these could be negative or positive)
% Xk = vector of complex amplitudes: Amp*e*exp(j*phase)
% fs = the number of samples per second for the time axis
% dur = total time duration of the signal
% tstart = starting time(default is zero, if you make this input
% optional)
% xx = vector of sinusoidal values
% tt = vector of times, for the time axis
%
%
% note: fk and XK must be the same length.
% Xk(1) = corresponds to frequency in fk(1),
% Xk(2) = corresponds to frequency in fk(2), etc
t = (tstart:1/(fs):dur);
x = Xk .* cos((fk * pi) .* t);
size(Xk)
size(fk)
plot (x,t);
shg
the size of Xk = 1 3
the size of fk = 1 3
I figured I would check the dimensions.

2 个评论

what is the size of t?
Next time, select your code and click on the {} Code button to format it properly, like I've done for you this time.

请先登录,再进行评论。

回答(2 个)

If xk and fk have the same size, then obviously, it's the third vector t that is a different size.
I don't see how you guarantee that t is the same length as Xk and fk. Moreover, your t calculation looks wrong, you're going from a point in time tstart to a duration dur. Maybe you meant:
t = tstart:1/fs:tstart + dur;
This still does not guarantee that t has the same number of elements as xk.
This should solve the different size issue:
t = linspace(tstart, tstart+dur, numel(Xk));

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by