Error using * Inner matrix dimensions must agree.

1 view (last 30 days)
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;
Arrays have incompatible sizes for this operation.
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
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
Name Size Bytes Class Attributes F 1x1 8 double T 1x1 8 double cmdout 1x33 66 char n 1x11 88 double t 1x500 4000 double term1 1x1 8 double term2 1x11 88 double term3 1x500 4000 double term4 1x11 88 double xn 1x11 88 double
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
Name Size Bytes Class Attributes innerSection 500x11 44000 double ya1 500x11 44000 double
But as written in your original code, MATLAB cannot multiply term2 and term3.
ya = sinc(term1.*term2.*term3).*term4;
Arrays have incompatible sizes for this operation.

Star Strider
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]);
.

Tags

Community Treasure Hunt

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

Start Hunting!