Error using * Inner matrix dimensions must agree.
1 view (last 30 days)
Show older comments
I have a problem that is already solved (the problem in the teacher's book) wrong, when I add * it gives me the following error:
"Matrix dimensions must agree."
I understand that it is about the size of the 2 matrices, what I don't understand ii how can I correct it?
The example statement is as follows:
Consider a sinusoidal signal having a frequency of 3 Hz. Write a program that will display the continuous signal and the samples when sampling with sampling frequencies of 100 Hz, 50 Hz, 10 Hz and 6 Hz.
In the model in the teacher's book there is the written version as a commentary.
clc
clear all
close all
F = 13;
T = 0.1;
n = (0:T:1);
xn = cos(2*pi*F*n);
t = linspace(-0.5, 1.5, 500);
% ya = sinc((1/T)*t(:,ones(size(n)))(1/T)*n(:,ones(size(t))))*xn;
ya = sinc((1/T).*t(:,ones(size(n)))*(1/T).*n(:,ones(size(t)))).*xn;
stem(n,xn,'r')
hold on
plot(t,ya)
grid on
xlabel('Timp, msec');
ylabel('Amplitudine');
axis([0 1 -1.2 1.2]);
Answers (2)
Steven Lord
on 8 Nov 2022
F = 13;
T = 0.1;
n = (0:T:1);
xn = cos(2*pi*F*n);
t = linspace(-0.5, 1.5, 500);
Now let's look at the terms you're multiplying on the next line.
term1 = (1/T);
term2 = t(:,ones(size(n)))*(1/T);
term3 = n(:,ones(size(t)));
term4 = xn;
What are their sizes?
whos
Elementwise multiplication of a scalar (term1) and an array (term2) is allowed. That product will be the same size as the array term2, namely 1-by-11.
What should be the size of the result of multiplying a 1-by-11 and a 1-by-500 array element-wise be? That operation is not defined mathematically, nor does it satisfy the requirements for implicit expansion to apply. See also this blog post from 2016 for more information on implicit expansion.
If that product in the sinc call returned a something-by-11 then implicit expansion would apply and ya would be something-by-11. But right now it's not defined. If you wanted that result to be 500-by-11, transposing term3 would work.
innerSection = term1.*term2.*term3.';
ya1 = sinc(innerSection).*term4;
whos innerSection ya1
But as written in your original code, MATLAB cannot multiply term2 and term3.
ya = sinc(term1.*term2.*term3).*term4;
0 Comments
Star Strider
on 8 Nov 2022
Change the ‘t’ assignment to:
t = linspace(-0.5, 1.5, numel(n));
and it works —
% clc
% clear all
% close all
F = 13;
T = 0.1;
n = (0:T:1);
xn = cos(2*pi*F*n);
t = linspace(-0.5, 1.5, numel(n));
% ya = sinc((1/T)*t(:,ones(size(n)))(1/T)*n(:,ones(size(t))))*xn;
ya = sinc((1/T).*t(:,ones(size(n)))*(1/T).*n(:,ones(size(t)))).*xn;
stem(n,xn,'r')
hold on
plot(t,ya)
grid on
xlabel('Timp, msec');
ylabel('Amplitudine');
axis([0 1 -1.2 1.2]);
.
0 Comments
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!